Fix _onModelEvent regression

Fixes #3662.
This commit is contained in:
Justin Ridgewell
2015-10-22 11:06:39 -04:00
parent fee53ab464
commit 67667c5b8f
2 changed files with 20 additions and 8 deletions

View File

@@ -1148,14 +1148,16 @@
// events simply proxy through. "add" and "remove" events that originate
// in other collections are ignored.
_onModelEvent: function(event, model, collection, options) {
if ((event === 'add' || event === 'remove') && collection !== this) return;
if (event === 'destroy') this.remove(model, options);
if (event === 'change') {
var prevId = this.modelId(model.previousAttributes());
var id = this.modelId(model.attributes);
if (prevId !== id) {
if (prevId != null) delete this._byId[prevId];
if (id != null) this._byId[id] = model;
if (model) {
if ((event === 'add' || event === 'remove') && collection !== this) return;
if (event === 'destroy') this.remove(model, options);
if (event === 'change') {
var prevId = this.modelId(model.previousAttributes());
var id = this.modelId(model.attributes);
if (prevId !== id) {
if (prevId != null) delete this._byId[prevId];
if (id != null) this._byId[id] = model;
}
}
}
this.trigger.apply(this, arguments);

View File

@@ -1803,4 +1803,14 @@
collection.invoke('method', 1, 2, 3);
});
QUnit.test('#3662 - triggering change without model will not error', function(assert) {
assert.expect(1);
var collection = new Backbone.Collection([{id: 1}]);
var model = collection.first();
collection.on('change', function(model) {
assert.equal(model, undefined);
});
model.trigger('change');
});
})();