Skip to content

Commit

Permalink
null and undefined are invalid ids.
Browse files Browse the repository at this point in the history
  • Loading branch information
braddunbar committed May 2, 2012
1 parent e0d349a commit 46d43ae
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion backbone.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
10 changes: 10 additions & 0 deletions test/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
});

});

0 comments on commit 46d43ae

Please sign in to comment.