@@ -2558,6 +2558,7 @@ function destroy(resourceName, id, options) {
2558
2558
2559
2559
options = options || { } ;
2560
2560
2561
+ id = DS . utils . resolveId ( definition , id ) ;
2561
2562
if ( ! definition ) {
2562
2563
throw new DS . errors . NER ( errorPrefix ( resourceName , id ) + resourceName ) ;
2563
2564
} else if ( ! DS . utils . isString ( id ) && ! DS . utils . isNumber ( id ) ) {
@@ -3181,17 +3182,17 @@ function loadRelations(resourceName, instance, relations, options) {
3181
3182
var params = { } ;
3182
3183
params [ def . foreignKey ] = instance [ definition . idAttribute ] ;
3183
3184
3184
- if ( def . type === 'hasMany' ) {
3185
+ if ( def . type === 'hasMany' && params [ def . foreignKey ] ) {
3185
3186
task = DS . findAll ( relationName , params , options ) ;
3186
3187
} else if ( def . type === 'hasOne' ) {
3187
3188
if ( def . localKey && instance [ def . localKey ] ) {
3188
3189
task = DS . find ( relationName , instance [ def . localKey ] , options ) ;
3189
- } else if ( def . foreignKey ) {
3190
+ } else if ( def . foreignKey && params [ def . foreignKey ] ) {
3190
3191
task = DS . findAll ( relationName , params , options ) . then ( function ( hasOnes ) {
3191
3192
return hasOnes . length ? hasOnes [ 0 ] : null ;
3192
3193
} ) ;
3193
3194
}
3194
- } else {
3195
+ } else if ( instance [ def . localKey ] ) {
3195
3196
task = DS . find ( relationName , instance [ def . localKey ] , options ) ;
3196
3197
}
3197
3198
@@ -3282,6 +3283,7 @@ function refresh(resourceName, id, options) {
3282
3283
3283
3284
options = options || { } ;
3284
3285
3286
+ id = DS . utils . resolveId ( DS . definitions [ resourceName ] , id ) ;
3285
3287
if ( ! DS . definitions [ resourceName ] ) {
3286
3288
throw new DS . errors . NER ( errorPrefix ( resourceName , id ) + resourceName ) ;
3287
3289
} else if ( ! DS . utils . isString ( id ) && ! DS . utils . isNumber ( id ) ) {
@@ -3367,6 +3369,7 @@ function save(resourceName, id, options) {
3367
3369
3368
3370
options = options || { } ;
3369
3371
3372
+ id = DS . utils . resolveId ( definition , id ) ;
3370
3373
if ( ! definition ) {
3371
3374
throw new DS . errors . NER ( errorPrefix ( resourceName , id ) + resourceName ) ;
3372
3375
} else if ( ! DS . utils . isString ( id ) && ! DS . utils . isNumber ( id ) ) {
@@ -3519,6 +3522,7 @@ function update(resourceName, id, attrs, options) {
3519
3522
3520
3523
options = options || { } ;
3521
3524
3525
+ id = DS . utils . resolveId ( definition , id ) ;
3522
3526
if ( ! definition ) {
3523
3527
throw new DS . errors . NER ( errorPrefix ( resourceName , id ) + resourceName ) ;
3524
3528
} else if ( ! DS . utils . isString ( id ) && ! DS . utils . isNumber ( id ) ) {
@@ -4605,6 +4609,7 @@ function bindOne(scope, expr, resourceName, id, cb) {
4605
4609
var DS = this ;
4606
4610
var IA = DS . errors . IA ;
4607
4611
4612
+ id = DS . utils . resolveId ( DS . definitions [ resourceName ] , id ) ;
4608
4613
if ( ! DS . utils . isObject ( scope ) ) {
4609
4614
throw new IA ( errorPrefix ( resourceName ) + 'scope: Must be an object!' ) ;
4610
4615
} else if ( ! DS . utils . isString ( expr ) ) {
@@ -4681,6 +4686,8 @@ function changeHistory(resourceName, id) {
4681
4686
var DSUtils = DS . utils ;
4682
4687
var definition = DS . definitions [ resourceName ] ;
4683
4688
var resource = DS . store [ resourceName ] ;
4689
+
4690
+ id = DS . utils . resolveId ( definition , id ) ;
4684
4691
if ( resourceName && ! DS . definitions [ resourceName ] ) {
4685
4692
throw new DS . errors . NER ( errorPrefix ( resourceName ) + resourceName ) ;
4686
4693
} else if ( id && ! DSUtils . isString ( id ) && ! DSUtils . isNumber ( id ) ) {
@@ -4745,6 +4752,8 @@ function errorPrefix(resourceName) {
4745
4752
*/
4746
4753
function changes ( resourceName , id ) {
4747
4754
var DS = this ;
4755
+
4756
+ id = DS . utils . resolveId ( DS . definitions [ resourceName ] , id ) ;
4748
4757
if ( ! DS . definitions [ resourceName ] ) {
4749
4758
throw new DS . errors . NER ( errorPrefix ( resourceName ) + resourceName ) ;
4750
4759
} else if ( ! DS . utils . isString ( id ) && ! DS . utils . isNumber ( id ) ) {
@@ -4764,6 +4773,11 @@ function changes(resourceName, id) {
4764
4773
} ) ;
4765
4774
diff [ name ] = DS . utils . pick ( diff [ name ] , toKeep ) ;
4766
4775
} ) ;
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
+ } ) ;
4767
4781
return diff ;
4768
4782
}
4769
4783
}
@@ -4844,6 +4858,7 @@ function compute(resourceName, instance) {
4844
4858
var IA = DS . errors . IA ;
4845
4859
var definition = DS . definitions [ resourceName ] ;
4846
4860
4861
+ instance = DS . utils . resolveItem ( DS . store [ resourceName ] , instance ) ;
4847
4862
if ( ! definition ) {
4848
4863
throw new DS . errors . NER ( errorPrefix ( resourceName ) + resourceName ) ;
4849
4864
} else if ( ! DS . utils . isObject ( instance ) && ! DS . utils . isString ( instance ) && ! DS . utils . isNumber ( instance ) ) {
@@ -5404,6 +5419,8 @@ function _eject(definition, resource, id) {
5404
5419
function eject ( resourceName , id ) {
5405
5420
var DS = this ;
5406
5421
var definition = DS . definitions [ resourceName ] ;
5422
+
5423
+ id = DS . utils . resolveId ( definition , id ) ;
5407
5424
if ( ! definition ) {
5408
5425
throw new DS . errors . NER ( errorPrefix ( resourceName , id ) + resourceName ) ;
5409
5426
} else if ( ! DS . utils . isString ( id ) && ! DS . utils . isNumber ( id ) ) {
@@ -5733,6 +5750,8 @@ function diffIsEmpty(utils, diff) {
5733
5750
*/
5734
5751
function hasChanges ( resourceName , id ) {
5735
5752
var DS = this ;
5753
+
5754
+ id = DS . utils . resolveId ( DS . definitions [ resourceName ] , id ) ;
5736
5755
if ( ! DS . definitions [ resourceName ] ) {
5737
5756
throw new DS . errors . NER ( errorPrefix ( resourceName , id ) + resourceName ) ;
5738
5757
} else if ( ! DS . utils . isString ( id ) && ! DS . utils . isNumber ( id ) ) {
@@ -6320,6 +6339,8 @@ function errorPrefix(resourceName, id) {
6320
6339
function lastModified ( resourceName , id ) {
6321
6340
var DS = this ;
6322
6341
var resource = DS . store [ resourceName ] ;
6342
+
6343
+ id = DS . utils . resolveId ( DS . definitions [ resourceName ] , id ) ;
6323
6344
if ( ! DS . definitions [ resourceName ] ) {
6324
6345
throw new DS . errors . NER ( errorPrefix ( resourceName , id ) + resourceName ) ;
6325
6346
} else if ( id && ! DS . utils . isString ( id ) && ! DS . utils . isNumber ( id ) ) {
@@ -6385,6 +6406,8 @@ function errorPrefix(resourceName, id) {
6385
6406
function lastSaved ( resourceName , id ) {
6386
6407
var DS = this ;
6387
6408
var resource = DS . store [ resourceName ] ;
6409
+
6410
+ id = DS . utils . resolveId ( DS . definitions [ resourceName ] , id ) ;
6388
6411
if ( ! DS . definitions [ resourceName ] ) {
6389
6412
throw new DS . errors . NER ( errorPrefix ( resourceName , id ) + resourceName ) ;
6390
6413
} else if ( ! DS . utils . isString ( id ) && ! DS . utils . isNumber ( id ) ) {
@@ -6475,6 +6498,7 @@ function link(resourceName, id, relations) {
6475
6498
6476
6499
relations = relations || [ ] ;
6477
6500
6501
+ id = DS . utils . resolveId ( definition , id ) ;
6478
6502
if ( ! definition ) {
6479
6503
throw new DS . errors . NER ( errorPrefix ( resourceName ) + resourceName ) ;
6480
6504
} else if ( ! DS . utils . isString ( id ) && ! DS . utils . isNumber ( id ) ) {
@@ -6688,6 +6712,8 @@ function linkInverse(resourceName, id, relations) {
6688
6712
6689
6713
relations = relations || [ ] ;
6690
6714
6715
+
6716
+ id = DS . utils . resolveId ( definition , id ) ;
6691
6717
if ( ! definition ) {
6692
6718
throw new DS . errors . NER ( errorPrefix ( resourceName ) + resourceName ) ;
6693
6719
} else if ( ! DS . utils . isString ( id ) && ! DS . utils . isNumber ( id ) ) {
@@ -6755,6 +6781,8 @@ function errorPrefix(resourceName, id) {
6755
6781
*/
6756
6782
function previous ( resourceName , id ) {
6757
6783
var DS = this ;
6784
+
6785
+ id = DS . utils . resolveId ( DS . definitions [ resourceName ] , id ) ;
6758
6786
if ( ! DS . definitions [ resourceName ] ) {
6759
6787
throw new DS . errors . NER ( errorPrefix ( resourceName , id ) + resourceName ) ;
6760
6788
} else if ( ! DS . utils . isString ( id ) && ! DS . utils . isNumber ( id ) ) {
@@ -6841,6 +6869,7 @@ function unlinkInverse(resourceName, id, relations) {
6841
6869
6842
6870
relations = relations || [ ] ;
6843
6871
6872
+ id = DS . utils . resolveId ( definition , id ) ;
6844
6873
if ( ! definition ) {
6845
6874
throw new DS . errors . NER ( errorPrefix ( resourceName ) + resourceName ) ;
6846
6875
} else if ( ! DS . utils . isString ( id ) && ! DS . utils . isNumber ( id ) ) {
@@ -7156,6 +7185,22 @@ module.exports = [function () {
7156
7185
remove : require ( 'mout/array/remove' ) ,
7157
7186
slice : require ( 'mout/array/slice' ) ,
7158
7187
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
+ } ,
7159
7204
updateTimestamp : function ( timestamp ) {
7160
7205
var newTimestamp = typeof Date . now === 'function' ? Date . now ( ) : new Date ( ) . getTime ( ) ;
7161
7206
if ( timestamp && newTimestamp <= timestamp ) {
0 commit comments