mirror of
https://github.com/jashkenas/backbone.git
synced 2026-04-08 03:00:26 -04:00
Issue #246 -- never fire nested change events for the same model. The top-level one will do.
This commit is contained in:
@@ -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;
|
||||
},
|
||||
|
||||
|
||||
@@ -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'});
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user