null and undefined are invalid ids.

This commit is contained in:
Brad Dunbar
2012-05-02 17:31:25 -04:00
parent e0d349a548
commit 46d43ae5bf
2 changed files with 11 additions and 1 deletions

View File

@@ -854,7 +854,7 @@
}
if (model && event === 'change:' + model.idAttribute) {
delete this._byId[model.previous(model.idAttribute)];
this._byId[model.id] = model;
if (model.id != null) this._byId[model.id] = model;
}
this.trigger.apply(this, arguments);
}

View File

@@ -589,4 +589,14 @@ $(document).ready(function() {
ok(c.at(0) instanceof Model);
});
test("null and undefined are invalid ids.", function() {
var model = new Backbone.Model({id: 1});
var collection = new Backbone.Collection([model]);
model.set({id: null});
ok(!collection.get('null'));
model.set({id: 1});
model.set({id: undefined});
ok(!collection.get('undefined'));
});
});