Issue #246 -- never fire nested change events for the same model. The top-level one will do.

This commit is contained in:
Jeremy Ashkenas
2011-05-27 10:29:14 -04:00
parent ed636326dd
commit 9accf97e9e
2 changed files with 32 additions and 1 deletions

View File

@@ -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;
},

View File

@@ -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'});
});
});