Skip to content

Commit

Permalink
Merge pull request #28 from daffl/master
Browse files Browse the repository at this point in the history
Fix for can.Model.model to accomodate custom ids
  • Loading branch information
daffl committed Apr 10, 2012
2 parents 234fd3b + beb9d2e commit ab04222
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
4 changes: 2 additions & 2 deletions model/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -725,9 +725,9 @@ steal('can/observe',function(){
if ( attributes instanceof this ) {
attributes = attributes.serialize();
}
var model = this.store[attributes.id] || new this( attributes );
var model = this.store[attributes[this.id]] || new this( attributes );
if(this._reqs){
this.store[attributes.id] = model;
this.store[attributes[this.id]] = model;
}
return model;
}
Expand Down
18 changes: 18 additions & 0 deletions model/model_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,24 @@ test("models", function(){
equals(people[0].prettyName(),"Mr. Justin","wraps wrapping works")
});

test(".models with custom id", function() {
can.Model("CustomId", {
findAll : steal.root.join("can/model/test") + "/customids.json",
id : '_id'
}, {
getName : function() {
return this.name;
}
});
stop();
CustomId.findAll().done(function(results) {
equals(results.length, 2, 'Got two items back');
equals(results[0].name, 'Justin', 'First name right');
equals(results[1].name, 'Brian', 'Second name right');
start();
});
});


/*
test("async setters", function(){
Expand Down
7 changes: 7 additions & 0 deletions model/test/customids.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[{
"_id" : 1,
"name" : "Justin"
}, {
"_id" : 2,
"name" : "Brian"
}]

0 comments on commit ab04222

Please sign in to comment.