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

  • Home
  • SEARCH
  • 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 6710105
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:57:41+00:00 2026-05-26T07:57:41+00:00

When either of two attributes (a or b) change in my Backbone model, I’d

  • 0

When either of two attributes (“a” or “b”) change in my Backbone model, I’d like to calculate a third attribute “c”:

initialize: function() {
  this.bind("change:a", this.calculateC);
  this.bind("change:b", this.calculateC);
},

calculateC: function() {
  this.attributes.c = ...
}   

What is a good way of preventing c from being calculated twice if both a and b are set on the model simultaneously?

  • 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-26T07:57:42+00:00Added an answer on May 26, 2026 at 7:57 am

    The attributes will not be set simultaneously, they will be set one at time so you need to see both events. The relevant code looks like this:

    // Set a hash of model attributes on the object, firing `"change"` unless you
    // choose to silence it.
    set : function(attrs, options) {
      // ...
    
      // Update attributes.
      for (var attr in attrs) {
        var val = attrs[attr];
        if (!_.isEqual(now[attr], val)) {
          now[attr] = val;
          // ...
          if (!options.silent) this.trigger('change:' + attr, this, val, options);
        }
      }
    
      // Fire the `"change"` event, if the model has been changed.
      if (!alreadyChanging && !options.silent && this._changed) this.change(options);
    

    You can see that one of the attributes is set, then its change event is triggered, then the process is repeated for the next attribute. If you only want one event, then you should bind only to the whole model’s change event.

    For completeness I should mention that the interface documentation for Model#set doesn’t specify any particular behavior as to when the individual change events will be triggered, it only says that they will be triggered.

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

Sidebar

Related Questions

If either of two values - a or b - change in my model,
I have two select lists, I would like jquery to either remove or disable
I have two tables looking like this: A B id_attr value id id_attr value
Generalities I'd like to append a random string to an element's attribute, using either
I'm trying to open a document in either of two files using the below
I have two cases to match: either # ... ([0-9]+,[0-9]+) € ... # or #
Is there anyway to batch two different select queries to either SQLite or SQLCE
If I have a data capture form with either tapstrips and or two columns
How to display a GridView from database matching either one of the two conditions.
The AbstractMavenMojo's execute method declares it throws two exceptions, MojoExecutionException and MojoFailureException. Throwing either

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.