Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8731245
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:11:29+00:00 2026-06-13T09:11:29+00:00

Wondering if any one can help me out here. I have a single page

  • 0

Wondering if any one can help me out here.

I have a single page on the client side running backbone.js

The server is running socket.io.

I have replaced Backbones default sync with the backbone.iobind sync to support socket.io.

I have a backbone model that on save gets an error response from the server (which is planned), and triggers the save’s error function, however my model on the client side still updates to the new value. I have included wait:true which from my understanding waits for a success response from the server before updating, however this does not happen for me.

Server side socket.io code

io.sockets.on('connection', function (socket) {
  socket.on('test:create', function (data, callback) {
    //err - used to fake an error for testing purposes
    var err = true;
    if (err) {
      // error
      callback('serv error');
    } else {
      // success
      // The callback accepts two parameters: error and model. If no error has occurred, send null for error.
      callback(null, data);
    }
  });

Client Side Code

  //HTML Code
  //<div id="callBtns">
    //<button id="runTest">Create</button>
  //</div>


  var CommunicationType = Backbone.Model.extend({
    defaults: {
      runTest: 'default'
    },
    urlRoot : '/test',
    validate: function(attrs) {
            //return 'error';    
    }
  }); 

  var BasicView = Backbone.View.extend({    
    el: $('#callBtns'), 

    initialize: function(){
      _.bindAll(this); 
    },

    events: {
    "click #runTest": "runTestFn"
    },

    runTestFn: function() {

      //causing the issue?
      communicationType.set({runTest: 'value from set'});     

      communicationType.save({},{
        success:function(model, serverResponse){
          console.log('Success');
          console.log(serverResponse);
        },
        error:function(model, serverResponse){
          console.log('Error');
          console.log(serverResponse);
          console.log(JSON.stringify(communicationType));
        },
        wait: true
      });
    }

  });

  var communicationType = new CommunicationType(); 
  var basicView = new BasicView();

I have noticed that this problem looks to be caused by the fact that I’m using set to update my model directly (communicationType.set({runTest: ‘value from set’});)

If I don’t set any values before saving, and pass the value in directly to save, like in the code below, it works correctly e.g.

communicationType.save({runTest: 'value from save'},{
            success:function(....

However this then means that I can’t ever set any new values/update my model without it going via the save function :/

So my question is, how can I get this working correctly (get the client model to not update if there is a server-side error) while still being able to use set?

Any answers are really appreciated! Many Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T09:11:31+00:00Added an answer on June 13, 2026 at 9:11 am

    This can’t be done because your model has no way of knowing if it’s data is valid until a request is made (model.save in this case).

    If you want to prevent Backbone.Model.set from working when you pass invalid values, you must update the model’s “validate” function to check for errors.

    Given the following validate function:

    validate: function(attrs) {
        this.errors = [];
        if (attrs.runTest === 'wrong_value') {
          this.errors.push('error message');
        }
        return _.any(this.errors) ? this.errors : null;
    }
    

    on your CommunicationType model. Running this code in BasicView:

    communicationType.set({runTest: 'wrong_value'});
    

    will not change the model.

    You can also use the validate function to check for server side errors by returning a errors array and checking for it:

    validate: function(attrs) {
        this.errors = [];
        if (attrs.runTest === 'wrong_value') { // client side check
          this.errors.push('error message');
        }
        if (attrs.errors && attrs.errors.length > 0) { // server side check
            for (var key in attrs.errors) {
                this.errors.push(attrs.errors[key]);
            }
        }
        return _.any(this.errors) ? this.errors : null;
    }
    

    But this will only prevent changing the model when it is saved.

    As a hack-ish alternative, you can add server requests inside the validate function that check for errors and prevent the “set” function from changing the model.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a few questions here if anyone can help me out. First off,
i was wondering if any one can advise me on how i can go
I'm stuck with recursion, was wondering if anyone can help me out with it.
I'm wondering if anybody can help? I have a large number of excel files.
I can't get my tests to work and was wondering if anyone have any
I'm wondering if anyone can help me out with some Canvas. I'm pretty new
Wondering if anyone can help me out. How can I line the content area
Was wondering if anyone could help me out here. I don't dont much with
I was wondering if anyone can point me in the right direction. I have
Just wondering if anyone has any ideas on how we can style templates... Like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.