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 7188413
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:04:45+00:00 2026-05-28T19:04:45+00:00

I have noticed that when multiple attributes of a Backbone model are set like

  • 0

I have noticed that when multiple attributes of a Backbone model are set like so

model.set({
    att1:val1,
    att2:val2
});

two change events are triggered. I was wrongly assuming that only one change event would be triggered after all the attributes had been set.

This might not seem like a problem, but it is when a function is bound to att1 that also uses the value of att2. In other words, when you do this

model.bind('change:att1', func1);
...
func1 = function() {
    var att2 = model.get('att2');
}

the variable att2 will be set to the old value of the model’s attribute att2.

The question is how to prevent this in an elegant manner. Of course, one option is to set att2 before setting att1 or to bind to att2 (instead of att1), but it seems that this is only a viable option in simple situations. The latter option also assumes that the attributes are set in the order in which they are listed in the set method (which is the case I think).

I have run into this issue several times hence my question. The issue is that it took me some time to realize what was actually happening.

On a final note, just like you can pass {silent:true} as an option of the set method, it would be nice to have an option {group:true} (or something like that) indicating that the change events should only be fired after all the attributes have been set.

  • 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-05-28T19:04:45+00:00Added an answer on May 28, 2026 at 7:04 pm

    In more complex situations i’d go for custom events.

    instead of binding to a change:att1 or change:att2 i’d look for a specific custom event, that you trigger after you have set all attributes you wanted to change on the model.

    model.set({
        att1:val1,
        att2:val2
    });
    model.trigger('contact:updated'); // you can chose your custom event name yourself
    
    model.bind('contact:updated', func1);
    ...
    func1 = function() {
        var att2 = model.get('att2');
    }
    

    downside on this idea is you have to add a new line of code everywhere you want to trigger the event. if this happens alot you might like to change or override the model.set() to do it for you, but then you’re already changing backbone code, don’t know how you feel about that.

    EDIT

    after looking into the sourcecode of backbone, i noticed the change event is triggered right after the change:attribute triggers. (proven by the snippit below)

    // Fire `change:attribute` events.
    for (var attr in changes) {
      if (!options.silent) this.trigger('change:' + attr, this, changes[attr], options);
    }
    
    // Fire the `"change"` event, if the model has been changed.
    if (!alreadyChanging) {
      if (!options.silent && this._changed) this.change(options);
      this._changing = false;
    }
    

    while the this.change(options); refers to this:

    change: function(options) {
      this.trigger('change', this, options);
      this._previousAttributes = _.clone(this.attributes);
      this._changed = false;
    },
    

    so if you would be binding to the change event instead of the specific change:argument event, you will arrive at a callback function after both (or all) attributes are changed.

    the only downside is, it will trigger on ANY change, even if you change a third or fourth attribute. you need to calculate that in…

    small example of how it works on jsfiddle http://jsfiddle.net/saelfaer/qm8xY/

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

Sidebar

Related Questions

I have noticed that sometimes people have to use multiple versions of jQuery in
I have noticed that some apps like Safari and Mail show a loading indicator
I have a controller with multiple actions that take :year and :month as attributes
I am new to C++ coding. I have noticed that if I enter multiple
I have noticed that there are multiple GL libraries labeled GL11, GL12,GL13,GL14 and so
I have a cart model class that has a List property like so: public
I have a set of model objects that have a public IsVisible boolean property.
I have noticed that cURL in PHP returns different data when told to output
I have noticed that regardless of a given script's execution time, every date() call
I have noticed that my particular instance of Trac is not running quickly and

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.