[Pkg-javascript-commits] [backbone] 171/211: Issue #246 -- never fire nested change events for the same model. The top-level one will do.

Jonas Smedegaard js at moszumanska.debian.org
Sat May 3 17:00:20 UTC 2014


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

js pushed a commit to tag 0.5.0
in repository backbone.

commit 9accf97e9eb1d9dc1701aea8a17a6959f3560c62
Author: Jeremy Ashkenas <jashkenas at gmail.com>
Date:   Fri May 27 10:29:14 2011 -0400

    Issue #246 -- never fire nested change events for the same model. The top-level one will do.
---
 backbone.js   |  7 ++++++-
 test/model.js | 26 ++++++++++++++++++++++++++
 2 files changed, 32 insertions(+), 1 deletion(-)

diff --git a/backbone.js b/backbone.js
index e88c829..0f440c4 100644
--- a/backbone.js
+++ b/backbone.js
@@ -204,6 +204,10 @@
       // Check for changes of `id`.
       if (this.idAttribute in attrs) this.id = attrs[this.idAttribute];
 
+      // We're about to start triggering change events.
+      var alreadyChanging = this._changing;
+      this._changing = true;
+
       // Update attributes.
       for (var attr in attrs) {
         var val = attrs[attr];
@@ -216,7 +220,8 @@
       }
 
       // Fire the `"change"` event, if the model has been changed.
-      if (!options.silent && this._changed) this.change(options);
+      if (!alreadyChanging && !options.silent && this._changed) this.change(options);
+      this._changing = false;
       return this;
     },
 
diff --git a/test/model.js b/test/model.js
index 7b4e9dd..34754f7 100644
--- a/test/model.js
+++ b/test/model.js
@@ -370,4 +370,30 @@ $(document).ready(function() {
     notEqual(Child.prototype.instancePropDiff, undefined);
   });
 
+  test("Model: Nested change events don't clobber previous attributes", function() {
+    var A = Backbone.Model.extend({
+      initialize: function() {
+        this.bind("change:state", function(a, newState) {
+          equals(a.previous('state'), undefined);
+          equals(newState, 'hello');
+          // Fire a nested change event.
+          this.set({ other: "whatever" });
+        });
+      }
+    });
+
+    var B = Backbone.Model.extend({
+      initialize: function() {
+        this.get("a").bind("change:state", function(a, newState) {
+          equals(a.previous('state'), undefined);
+          equals(newState, 'hello');
+        });
+      }
+    });
+
+    a = new A();
+    b = new B({a: a});
+    a.set({state: 'hello'});
+  });
+
 });

-- 
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