Skip to content

Commit

Permalink
ditto
Browse files Browse the repository at this point in the history
  • Loading branch information
jashkenas committed Feb 7, 2011
1 parent c5160fc commit c6d2c69
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
2 changes: 1 addition & 1 deletion backbone.js
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,7 @@
if (ev == 'destroy') {
this._remove(model, options);
}
if (ev === 'change:id') {
if (ev === 'change:' + model.idAttribute) {
delete this._byId[model.previous(model.idAttribute)];
this._byId[model.id] = model;
}
Expand Down
20 changes: 16 additions & 4 deletions test/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@ $(document).ready(function() {
equals(col.getByCid(col.first().cid), col.first());
});

test("Collection: get with non-default ids", function() {
var col = new Backbone.Collection();
var MongoModel = Backbone.Model.extend({
idAttribute: '_id'
});
var model = new MongoModel({_id: 100});
col.add(model);
equals(col.get(100), model);
model.set({_id: 101});
equals(col.get(101), model);
});

test("Collection: update index when id changes", function() {
var col = new Backbone.Collection();
col.add([
Expand Down Expand Up @@ -210,12 +222,12 @@ $(document).ready(function() {

test("Collection: create enforces validation", function() {
var ValidatingModel = Backbone.Model.extend({
validate: function(attrs) {
return "fail"
}
validate: function(attrs) {
return "fail";
}
});
var ValidatingCollection = Backbone.Collection.extend({
model: ValidatingModel
model: ValidatingModel
});
var col = new ValidatingCollection();
equals(col.create({"foo":"bar"}),false);
Expand Down

0 comments on commit c6d2c69

Please sign in to comment.