// -------------------------------------------------------------------------- \\
// File: ToOneAttribute.js                                                    \\
// Module: DataStore                                                          \\
// Requires: RecordAttribute.js                                               \\
// Author: Neil Jenkins                                                       \\
// License: © 2010–2012 Opera Software ASA. All rights reserved.              \\
// -------------------------------------------------------------------------- \\

/*global O */
 
"use strict";

( function ( NS, undefined ) {

var ToOneAttribute = NS.Class({
    
    Extends: NS.RecordAttribute,
    
    willCreateInStore: function ( record, propKey, storeKey ) {
        var propValue = record.get( propKey );
        if ( propValue && !propValue.get( 'id' ) ) {
            record.get( 'store' ).attrMapsToStoreKey(
                propValue.get( 'storeKey' ),
                storeKey, this.key || propKey );
        }
    },
    
    willSet: function ( propValue, propKey, record ) {
        if ( ToOneAttribute.parent.willSet.call(
                this, propValue, propKey, record ) ) {
            if ( !propValue.get( 'storeKey' ) ) {
                throw new Error( 'O.ToOneAttribute: ' +
                    'Cannot set connection to record not saved to store.' );
            }
            var oldPropValue = record.get( propKey ),
                storeKey = record.get( 'storeKey' ),
                store = record.get( 'store' ),
                attrKey = this.key || propKey;
            if ( storeKey ) {
                if ( oldPropValue && !oldPropValue.get( 'id' ) ) {
                    store.attrNoLongerMapsToStoreKey(
                        oldPropValue.get( 'storeKey' ),
                        storeKey, attrKey );
                }
                if ( propValue && !propValue.get( 'id' ) ) {
                    store.attrMapsToStoreKey(
                        propValue.get( 'storeKey' ),
                        storeKey, attrKey );
                }
            }
            return true;
        }
        return false;
    },
    
    call: function ( record, propValue, propKey ) {
        var result = ToOneAttribute.parent.call.call(
            this, record, propValue, propKey );
        if ( result && typeof result === 'string' ) {
            result = record.get( 'store' ).getRecord( this.type, result );
        }
        return result || null;
    }
});

NS.ToOneAttribute = ToOneAttribute;

NS.Record.toOne = function ( options ) {
    return new ToOneAttribute( options );
};

}( O ) );
@@ -1208,16 +1208,60 @@
     if (
+ propValue !== null ) %7B%0A                if (
  !propVa
@@ -1278,32 +1278,36 @@
 storeKey' ) ) %7B%0A
+    
                 
@@ -1366,16 +1366,20 @@
         
+   
+ 
 'Cannot 
@@ -1431,34 +1431,41 @@
  );%0A            
-%7D%0A
+    %7D%0A   
             var 
@@ -1456,24 +1456,25 @@
             
+ 
 var oldPropV
@@ -1511,32 +1511,36 @@
                 
+    
 storeKey = recor
@@ -1572,24 +1572,28 @@
             
+   
+ 
 store = reco
@@ -1607,24 +1607,28 @@
  'store' ),%0A
+    
             
@@ -1662,16 +1662,20 @@
 ropKey;%0A
+    
         
@@ -1708,24 +1708,28 @@
             
+    
 if ( oldProp
@@ -1759,32 +1759,36 @@
 get( 'id' ) ) %7B%0A
+    
                 
@@ -1849,16 +1849,20 @@
         
+    
 oldPropV
@@ -1901,32 +1901,36 @@
                 
+    
 storeKey, attrKe
@@ -1942,34 +1942,42 @@
                 
+ 
-%7D%0A
+   %7D%0A    
                 
@@ -2033,32 +2033,36 @@
                 
+ 
+   
 store.attrMapsTo
@@ -2063,32 +2063,36 @@
 MapsToStoreKey(%0A
+    
                 
@@ -2144,32 +2144,36 @@
                 
+    
 storeKey, attrKe
@@ -2169,32 +2169,54 @@
 Key, attrKey );%0A
+                    %7D%0A
                 

Allow null value to be set in ToOneAttribute