Skip to content

Commit

Permalink
Fixed a bug in SC.SingleAttribute that prevent proper use of a differ…
Browse files Browse the repository at this point in the history
…ent key
  • Loading branch information
wagenet authored and Charles Jolley committed Oct 23, 2009
1 parent bb8224c commit 1de24c6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
5 changes: 3 additions & 2 deletions frameworks/datastore/models/single_attribute.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ SC.SingleAttribute = SC.RecordAttribute.extend(
@private - implements support for handling inverse relationships.
*/
call: function(record, key, newRec) {
var inverseKey, isMaster, oldRec, attr, ret, nvalue;
var attrKey = this.get('key') || key,
inverseKey, isMaster, oldRec, attr, ret, nvalue;

// WRITE
if (newRec !== undefined) {
Expand All @@ -68,7 +69,7 @@ SC.SingleAttribute = SC.RecordAttribute.extend(
// careful: don't overwrite value here. we want the return value to
// cache.
nvalue = this.fromType(record, key, newRec) ; // convert to attribute.
record.writeAttribute(key, nvalue, !this.get('isMaster'));
record.writeAttribute(attrKey, nvalue, !this.get('isMaster'));
ret = newRec ;

// ok, now if we have an inverse relationship, get the inverse
Expand Down
23 changes: 22 additions & 1 deletion frameworks/datastore/tests/models/single_attribute.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ module("SC.RecordAttribute core methods", {
return (this.readAttribute('relatedToComputed').indexOf("foo")===0) ? MyApp.Foo : MyApp.Bar;
}),

bar: SC.Record.toOne('MyApp.Bar', { inverse: 'foo' })
bar: SC.Record.toOne('MyApp.Bar', { inverse: 'foo' }),

barKeyed: SC.Record.toOne('MyApp.Bar', { key: 'barId' })

});

Expand Down Expand Up @@ -65,6 +67,13 @@ module("SC.RecordAttribute core methods", {
anArray: ['one', 'two', 'three'],
anObject: { 'key1': 'value1', 'key2': 'value2' },
bar: "bar2"
},

{
guid: 'foo4',
firstName: 'Joe',
lastName: 'Schmo',
barId: 'bar1'
}

]);
Expand Down Expand Up @@ -108,6 +117,11 @@ test("reading an inverse relationship", function() {
equals(bar.get('foo'), rec, 'bar.foo should == foo1');
});

test("reading a keyed relationship", function(){
var rec4 = MyApp.store.find(MyApp.Foo, 'foo4');
equals(rec4.get('barKeyed'), bar, 'foo4.barKeyed should == bar');
});

// ..........................................................
// WRITING
//
Expand Down Expand Up @@ -214,3 +228,10 @@ test("modifying a toOne relationship with an inverse from other", function() {

});

test("modifying a keyed toOne relationship", function(){
var rec4 = MyApp.store.find(MyApp.Foo, 'foo4');

rec4.set('barKeyed', bar2);

equals(rec4.get('barId'), 'bar2', 'foo4.barId should == bar2');
});

0 comments on commit 1de24c6

Please sign in to comment.