@@ -2558,6 +2558,7 @@ function destroy(resourceName, id, options) {
25582558
25592559 options = options || { } ;
25602560
2561+ id = DS . utils . resolveId ( definition , id ) ;
25612562 if ( ! definition ) {
25622563 throw new DS . errors . NER ( errorPrefix ( resourceName , id ) + resourceName ) ;
25632564 } else if ( ! DS . utils . isString ( id ) && ! DS . utils . isNumber ( id ) ) {
@@ -3181,17 +3182,17 @@ function loadRelations(resourceName, instance, relations, options) {
31813182 var params = { } ;
31823183 params [ def . foreignKey ] = instance [ definition . idAttribute ] ;
31833184
3184- if ( def . type === 'hasMany' ) {
3185+ if ( def . type === 'hasMany' && params [ def . foreignKey ] ) {
31853186 task = DS . findAll ( relationName , params , options ) ;
31863187 } else if ( def . type === 'hasOne' ) {
31873188 if ( def . localKey && instance [ def . localKey ] ) {
31883189 task = DS . find ( relationName , instance [ def . localKey ] , options ) ;
3189- } else if ( def . foreignKey ) {
3190+ } else if ( def . foreignKey && params [ def . foreignKey ] ) {
31903191 task = DS . findAll ( relationName , params , options ) . then ( function ( hasOnes ) {
31913192 return hasOnes . length ? hasOnes [ 0 ] : null ;
31923193 } ) ;
31933194 }
3194- } else {
3195+ } else if ( instance [ def . localKey ] ) {
31953196 task = DS . find ( relationName , instance [ def . localKey ] , options ) ;
31963197 }
31973198
@@ -3282,6 +3283,7 @@ function refresh(resourceName, id, options) {
32823283
32833284 options = options || { } ;
32843285
3286+ id = DS . utils . resolveId ( DS . definitions [ resourceName ] , id ) ;
32853287 if ( ! DS . definitions [ resourceName ] ) {
32863288 throw new DS . errors . NER ( errorPrefix ( resourceName , id ) + resourceName ) ;
32873289 } else if ( ! DS . utils . isString ( id ) && ! DS . utils . isNumber ( id ) ) {
@@ -3367,6 +3369,7 @@ function save(resourceName, id, options) {
33673369
33683370 options = options || { } ;
33693371
3372+ id = DS . utils . resolveId ( definition , id ) ;
33703373 if ( ! definition ) {
33713374 throw new DS . errors . NER ( errorPrefix ( resourceName , id ) + resourceName ) ;
33723375 } else if ( ! DS . utils . isString ( id ) && ! DS . utils . isNumber ( id ) ) {
@@ -3519,6 +3522,7 @@ function update(resourceName, id, attrs, options) {
35193522
35203523 options = options || { } ;
35213524
3525+ id = DS . utils . resolveId ( definition , id ) ;
35223526 if ( ! definition ) {
35233527 throw new DS . errors . NER ( errorPrefix ( resourceName , id ) + resourceName ) ;
35243528 } else if ( ! DS . utils . isString ( id ) && ! DS . utils . isNumber ( id ) ) {
@@ -4605,6 +4609,7 @@ function bindOne(scope, expr, resourceName, id, cb) {
46054609 var DS = this ;
46064610 var IA = DS . errors . IA ;
46074611
4612+ id = DS . utils . resolveId ( DS . definitions [ resourceName ] , id ) ;
46084613 if ( ! DS . utils . isObject ( scope ) ) {
46094614 throw new IA ( errorPrefix ( resourceName ) + 'scope: Must be an object!' ) ;
46104615 } else if ( ! DS . utils . isString ( expr ) ) {
@@ -4681,6 +4686,8 @@ function changeHistory(resourceName, id) {
46814686 var DSUtils = DS . utils ;
46824687 var definition = DS . definitions [ resourceName ] ;
46834688 var resource = DS . store [ resourceName ] ;
4689+
4690+ id = DS . utils . resolveId ( definition , id ) ;
46844691 if ( resourceName && ! DS . definitions [ resourceName ] ) {
46854692 throw new DS . errors . NER ( errorPrefix ( resourceName ) + resourceName ) ;
46864693 } else if ( id && ! DSUtils . isString ( id ) && ! DSUtils . isNumber ( id ) ) {
@@ -4745,6 +4752,8 @@ function errorPrefix(resourceName) {
47454752 */
47464753function changes ( resourceName , id ) {
47474754 var DS = this ;
4755+
4756+ id = DS . utils . resolveId ( DS . definitions [ resourceName ] , id ) ;
47484757 if ( ! DS . definitions [ resourceName ] ) {
47494758 throw new DS . errors . NER ( errorPrefix ( resourceName ) + resourceName ) ;
47504759 } else if ( ! DS . utils . isString ( id ) && ! DS . utils . isNumber ( id ) ) {
@@ -4764,6 +4773,11 @@ function changes(resourceName, id) {
47644773 } ) ;
47654774 diff [ name ] = DS . utils . pick ( diff [ name ] , toKeep ) ;
47664775 } ) ;
4776+ DS . utils . forEach ( DS . definitions [ resourceName ] . relationFields , function ( field ) {
4777+ delete diff . added [ field ] ;
4778+ delete diff . removed [ field ] ;
4779+ delete diff . changed [ field ] ;
4780+ } ) ;
47674781 return diff ;
47684782 }
47694783}
@@ -4844,6 +4858,7 @@ function compute(resourceName, instance) {
48444858 var IA = DS . errors . IA ;
48454859 var definition = DS . definitions [ resourceName ] ;
48464860
4861+ instance = DS . utils . resolveItem ( DS . store [ resourceName ] , instance ) ;
48474862 if ( ! definition ) {
48484863 throw new DS . errors . NER ( errorPrefix ( resourceName ) + resourceName ) ;
48494864 } else if ( ! DS . utils . isObject ( instance ) && ! DS . utils . isString ( instance ) && ! DS . utils . isNumber ( instance ) ) {
@@ -5404,6 +5419,8 @@ function _eject(definition, resource, id) {
54045419function eject ( resourceName , id ) {
54055420 var DS = this ;
54065421 var definition = DS . definitions [ resourceName ] ;
5422+
5423+ id = DS . utils . resolveId ( definition , id ) ;
54075424 if ( ! definition ) {
54085425 throw new DS . errors . NER ( errorPrefix ( resourceName , id ) + resourceName ) ;
54095426 } else if ( ! DS . utils . isString ( id ) && ! DS . utils . isNumber ( id ) ) {
@@ -5733,6 +5750,8 @@ function diffIsEmpty(utils, diff) {
57335750 */
57345751function hasChanges ( resourceName , id ) {
57355752 var DS = this ;
5753+
5754+ id = DS . utils . resolveId ( DS . definitions [ resourceName ] , id ) ;
57365755 if ( ! DS . definitions [ resourceName ] ) {
57375756 throw new DS . errors . NER ( errorPrefix ( resourceName , id ) + resourceName ) ;
57385757 } else if ( ! DS . utils . isString ( id ) && ! DS . utils . isNumber ( id ) ) {
@@ -6320,6 +6339,8 @@ function errorPrefix(resourceName, id) {
63206339function lastModified ( resourceName , id ) {
63216340 var DS = this ;
63226341 var resource = DS . store [ resourceName ] ;
6342+
6343+ id = DS . utils . resolveId ( DS . definitions [ resourceName ] , id ) ;
63236344 if ( ! DS . definitions [ resourceName ] ) {
63246345 throw new DS . errors . NER ( errorPrefix ( resourceName , id ) + resourceName ) ;
63256346 } else if ( id && ! DS . utils . isString ( id ) && ! DS . utils . isNumber ( id ) ) {
@@ -6385,6 +6406,8 @@ function errorPrefix(resourceName, id) {
63856406function lastSaved ( resourceName , id ) {
63866407 var DS = this ;
63876408 var resource = DS . store [ resourceName ] ;
6409+
6410+ id = DS . utils . resolveId ( DS . definitions [ resourceName ] , id ) ;
63886411 if ( ! DS . definitions [ resourceName ] ) {
63896412 throw new DS . errors . NER ( errorPrefix ( resourceName , id ) + resourceName ) ;
63906413 } else if ( ! DS . utils . isString ( id ) && ! DS . utils . isNumber ( id ) ) {
@@ -6475,6 +6498,7 @@ function link(resourceName, id, relations) {
64756498
64766499 relations = relations || [ ] ;
64776500
6501+ id = DS . utils . resolveId ( definition , id ) ;
64786502 if ( ! definition ) {
64796503 throw new DS . errors . NER ( errorPrefix ( resourceName ) + resourceName ) ;
64806504 } else if ( ! DS . utils . isString ( id ) && ! DS . utils . isNumber ( id ) ) {
@@ -6688,6 +6712,8 @@ function linkInverse(resourceName, id, relations) {
66886712
66896713 relations = relations || [ ] ;
66906714
6715+
6716+ id = DS . utils . resolveId ( definition , id ) ;
66916717 if ( ! definition ) {
66926718 throw new DS . errors . NER ( errorPrefix ( resourceName ) + resourceName ) ;
66936719 } else if ( ! DS . utils . isString ( id ) && ! DS . utils . isNumber ( id ) ) {
@@ -6755,6 +6781,8 @@ function errorPrefix(resourceName, id) {
67556781 */
67566782function previous ( resourceName , id ) {
67576783 var DS = this ;
6784+
6785+ id = DS . utils . resolveId ( DS . definitions [ resourceName ] , id ) ;
67586786 if ( ! DS . definitions [ resourceName ] ) {
67596787 throw new DS . errors . NER ( errorPrefix ( resourceName , id ) + resourceName ) ;
67606788 } else if ( ! DS . utils . isString ( id ) && ! DS . utils . isNumber ( id ) ) {
@@ -6841,6 +6869,7 @@ function unlinkInverse(resourceName, id, relations) {
68416869
68426870 relations = relations || [ ] ;
68436871
6872+ id = DS . utils . resolveId ( definition , id ) ;
68446873 if ( ! definition ) {
68456874 throw new DS . errors . NER ( errorPrefix ( resourceName ) + resourceName ) ;
68466875 } else if ( ! DS . utils . isString ( id ) && ! DS . utils . isNumber ( id ) ) {
@@ -7156,6 +7185,22 @@ module.exports = [function () {
71567185 remove : require ( 'mout/array/remove' ) ,
71577186 slice : require ( 'mout/array/slice' ) ,
71587187 sort : require ( 'mout/array/sort' ) ,
7188+ resolveItem : function ( resource , idOrInstance ) {
7189+ if ( resource && ( this . isString ( idOrInstance ) || this . isNumber ( idOrInstance ) ) ) {
7190+ return resource . index [ idOrInstance ] || idOrInstance ;
7191+ } else {
7192+ return idOrInstance ;
7193+ }
7194+ } ,
7195+ resolveId : function ( definition , idOrInstance ) {
7196+ if ( this . isString ( idOrInstance ) || this . isNumber ( idOrInstance ) ) {
7197+ return idOrInstance ;
7198+ } else if ( idOrInstance && definition ) {
7199+ return idOrInstance [ definition . idAttribute ] || idOrInstance ;
7200+ } else {
7201+ return idOrInstance ;
7202+ }
7203+ } ,
71597204 updateTimestamp : function ( timestamp ) {
71607205 var newTimestamp = typeof Date . now === 'function' ? Date . now ( ) : new Date ( ) . getTime ( ) ;
71617206 if ( timestamp && newTimestamp <= timestamp ) {
0 commit comments