Skip to content
This repository

Changed the model's set function so that the 'nested' variable isn't req... #2022

Merged
merged 1 commit into from over 1 year ago

4 participants

Adriaan Labuschagne brad dunbar Tim Griesser Jeremy Ashkenas
Adriaan Labuschagne

...uired

There used to be a line that read: this._pending = !!changes.length;
This caused this._pending to sometimes be set to false when model.set is called from within a change event. It would, for instance, happen during this test:

 test("final `change` event is fired when interim changes set an attribute without changing it's value", 1, function () {
    var model = new Backbone.Model({foo: 1, bar: 2});
    model.on('change:foo', function() {
      model.set({bar: 2});
      console.log('property changeevnt');
    });
    model.on('change', function() {
      ok(true);
    });
    model.set({foo: 10});
  });

During model.set({bar: 2});, this._pending gets set to false and therefore no change event would get triggered (without the use of nested). nested was used to trigger this event like so:

if (!this._pending && nested) this.trigger('change', this, options);

This commit changes the method so that nested is no longer required.

Note: the test is't in the test sweet, and all the current tests pass. Merry Christmas.

Signed-off-by: Adriaan Labuschagne adriaanlabuschagne@gmail.com

Adriaan Labuschagne Changed the model's set function so that the 'nested' variable isn't …
…required

Signed-off-by: Adriaan Labuschagne <adriaanlabuschagne@gmail.com>
4f1cb85
brad dunbar
Collaborator

Fantastic. Wish I would've come up with it myself. :)

@jashkenas @tgriesser Unless you guys have any reason not to merge this, I think it looks good.

Tim Griesser tgriesser merged commit 323d216 into from December 26, 2012
Tim Griesser tgriesser closed this December 26, 2012
Tim Griesser
Collaborator

Looks good to me - thanks @adriaanlabusc!

Jeremy Ashkenas
Owner

Very, very nice.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Dec 25, 2012
Adriaan Labuschagne Changed the model's set function so that the 'nested' variable isn't …
…required

Signed-off-by: Adriaan Labuschagne <adriaanlabuschagne@gmail.com>
4f1cb85
This page is out of date. Refresh to see the latest.

Showing 1 changed file with 2 additions and 5 deletions. Show diff stats Hide diff stats

  1. 7  backbone.js
7  backbone.js
@@ -291,7 +291,7 @@
291 291
     // Set a hash of model attributes on the object, firing `"change"` unless
292 292
     // you choose to silence it.
293 293
     set: function(key, val, options) {
294  
-      var attr, attrs, unset, changes, silent, nested, changing, prev, current;
  294
+      var attr, attrs, unset, changes, silent, changing, prev, current;
295 295
       if (key == null) return this;
296 296
 
297 297
       // Handle both `"key", value` and `{key: value}` -style arguments.
@@ -308,7 +308,6 @@
308 308
       unset           = options.unset;
309 309
       silent          = options.silent;
310 310
       changes         = [];
311  
-      nested          = false;
312 311
       changing        = this._changing;
313 312
       this._changing  = true;
314 313
 
@@ -329,7 +328,6 @@
329 328
         val = attrs[attr];
330 329
         if (!_.isEqual(current[attr], val)) changes.push(attr);
331 330
         if (!_.isEqual(prev[attr], val)) {
332  
-          nested = true;
333 331
           this.changed[attr] = val;
334 332
         } else {
335 333
           delete this.changed[attr];
@@ -337,7 +335,7 @@
337 335
         unset ? delete current[attr] : current[attr] = val;
338 336
       }
339 337
 
340  
-      this._pending = !!changes.length;
  338
+      if (changes.length) this._pending = true;
341 339
 
342 340
       // Trigger all relevant attribute changes.
343 341
       if (!silent) {
@@ -348,7 +346,6 @@
348 346
 
349 347
       if (changing) return this;
350 348
       if (!silent) {
351  
-        if (!this._pending && nested) this.trigger('change', this, options);
352 349
         while (this._pending) {
353 350
           this._pending = false;
354 351
           this.trigger('change', this, options);
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.