Skip to content

Commit

Permalink
Improved setters for Record primaryKeys
Browse files Browse the repository at this point in the history
  • Loading branch information
wagenet authored and Charles Jolley committed Oct 23, 2009
1 parent 1de24c6 commit 57a9aa6
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
10 changes: 8 additions & 2 deletions frameworks/datastore/models/record.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,13 @@ SC.Record = SC.Object.extend(
@property {String}
*/
id: function() {
return SC.Store.idFor(this.storeKey);
id: function(key, value) {
if (value !== undefined) {
this.writeAttribute(this.get('primaryKey'), value);
return value;
} else {
return SC.Store.idFor(this.storeKey);
}
}.property('storeKey').cacheable(),

/**
Expand Down Expand Up @@ -302,6 +307,7 @@ SC.Record = SC.Object.extend(
// if value is primaryKey of record, write it to idsByStoreKey
if (key===this.get('primaryKey')) {
SC.Store.idsByStoreKey[storeKey] = attrs[key] ;
this.propertyDidChange('id'); // Reset computed value
}

return this ;
Expand Down
15 changes: 15 additions & 0 deletions frameworks/datastore/tests/models/record/writeAttribute.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,18 @@ test("Writing to an attribute in chained store sets correct status", function()

});


test("Writing a new guid", function(){
equals(foo.get('id'), 1, 'foo.id should be 1');
foo.set('guid', 2);
equals(foo.get('id'), 2, 'foo.id should be 2');
});

test("Writing primaryKey of 'id'", function(){
PrimaryKeyId = SC.Record.extend({ primaryKey: 'id' });
var foo2 = store.createRecord(PrimaryKeyId, { id: 1 });

equals(foo2.get('id'), 1, 'foo2.id should be 1');
foo2.set('id', 2);
equals(foo2.get('id'), 2, 'foo2.id should be 2');
});

0 comments on commit 57a9aa6

Please sign in to comment.