Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

hasChanged/set should use the same comparison #842

Merged
merged 1 commit into from

2 participants

@braddunbar
Collaborator

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.

@braddunbar braddunbar 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.
934aba4
@jashkenas jashkenas merged commit 060da2b into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 6, 2012
  1. @braddunbar

    hasChanged/set should use the same comparison

    braddunbar authored
    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.
This page is out of date. Refresh to see the latest.
Showing with 14 additions and 1 deletion.
  1. +1 −1  backbone.js
  2. +13 −0 test/model.js
View
2  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;
},
View
13 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);
+ });
+
});
Something went wrong with that request. Please try again.