[Pkg-javascript-commits] [backbone] 200/281: Fixes #582, fixes #425, run a second loop to trigger change:attribute events after all changes have been made.

Jonas Smedegaard js at moszumanska.debian.org
Sat May 3 17:02:12 UTC 2014


This is an automated email from the git hooks/post-receive script.

js pushed a commit to tag 0.9.0
in repository backbone.

commit 34e0f7fb26c3c2a13e7dfdc072b33fd7b971c72b
Author: Jeremy Ashkenas <jashkenas at gmail.com>
Date:   Fri Jan 13 17:08:48 2012 -0500

    Fixes #582, fixes #425, run a second loop to trigger change:attribute events after all changes have been made.
---
 backbone.js   | 14 ++++++++++----
 test/model.js | 16 ++++++++++++++++
 2 files changed, 26 insertions(+), 4 deletions(-)

diff --git a/backbone.js b/backbone.js
index cd2e9c4..0e7cd63 100644
--- a/backbone.js
+++ b/backbone.js
@@ -202,7 +202,7 @@
     // Set a hash of model attributes on the object, firing `"change"` unless
     // you choose to silence it.
     set : function(key, value, options) {
-      var attrs;
+      var attrs, attr, val;
       if (_.isObject(key) || key == null) {
         attrs = key;
         options = value;
@@ -229,16 +229,22 @@
       this._changing = true;
 
       // Update attributes.
-      for (var attr in attrs) {
-        var val = attrs[attr];
+      var changes = {};
+      for (attr in attrs) {
+        val = attrs[attr];
         if (!_.isEqual(now[attr], val) || (options.unset && (attr in now))) {
           options.unset ? delete now[attr] : now[attr] = val;
           delete escaped[attr];
           this._changed = true;
-          if (!options.silent) this.trigger('change:' + attr, this, val, options);
+          changes[attr] = val;
         }
       }
 
+      // 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);
diff --git a/test/model.js b/test/model.js
index ea7ac80..e60dba6 100644
--- a/test/model.js
+++ b/test/model.js
@@ -505,4 +505,20 @@ $(document).ready(function() {
     equal(changed, 1);
   });
 
+  test("#582, #425, change:attribute callbacks should fire after all changes have occurred", 9, function() {
+    var model = new Backbone.Model;
+
+    var assertion = function() {
+      equals(model.get('a'), 'a');
+      equals(model.get('b'), 'b');
+      equals(model.get('c'), 'c');
+    };
+
+    model.on('change:a', assertion);
+    model.on('change:b', assertion);
+    model.on('change:c', assertion);
+
+    model.set({a: 'a', b: 'b', c: 'c'});
+  });
+
 });

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/backbone.git



More information about the Pkg-javascript-commits mailing list