[Pkg-javascript-commits] [backbone] 147/281: hasChanged/set should use the same comparison
Jonas Smedegaard
js at moszumanska.debian.org
Sat May 3 17:02:06 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 934aba4c6e94ce619cdb7104d8c3491df3d817eb
Author: Brad Dunbar <dunbarb2 at gmail.com>
Date: Fri Jan 6 09:19:23 2012 -0500
hasChanged/set should use the same comparison
When setting a value, if hasChanged and set disagree
about equality then hasChanged will return true without
firing a 'changed:*' event (or vice versa). Using the
same comparison (_.isEqual) solves this problem.
---
backbone.js | 2 +-
test/model.js | 13 +++++++++++++
2 files changed, 14 insertions(+), 1 deletion(-)
diff --git a/backbone.js b/backbone.js
index 8438e72..1b5b112 100644
--- a/backbone.js
+++ b/backbone.js
@@ -325,7 +325,7 @@
// Determine if the model has changed since the last `"change"` event.
// If you specify an attribute name, determine if that attribute has changed.
hasChanged : function(attr) {
- if (attr) return this._previousAttributes[attr] != this.attributes[attr];
+ if (attr) return !_.isEqual(this._previousAttributes[attr], this.attributes[attr]);
return this._changed;
},
diff --git a/test/model.js b/test/model.js
index 85c6602..1daf100 100644
--- a/test/model.js
+++ b/test/model.js
@@ -480,4 +480,17 @@ $(document).ready(function() {
equal(counter, 1, 'change is only triggered once');
});
+ test("hasChanged/set should use same comparison", function() {
+ expect(2);
+ var changed = 0, model = new Backbone.Model({a: null});
+ model.bind('change', function() {
+ ok(this.hasChanged('a'));
+ })
+ .bind('change:a', function() {
+ changed++;
+ })
+ .set({a: undefined});
+ equal(changed, 1);
+ });
+
});
--
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