@@ -2403,6 +2403,16 @@ module.exports = {
2403
2403
*/
2404
2404
findAll : require ( './findAll' ) ,
2405
2405
2406
+ /**
2407
+ * @doc method
2408
+ * @id DS.async_methods:loadRelations
2409
+ * @name loadRelations
2410
+ * @methodOf DS
2411
+ * @description
2412
+ * See [DS.loadRelations](/documentation/api/api/DS.async_methods:loadRelations).
2413
+ */
2414
+ loadRelations : require ( './loadRelations' ) ,
2415
+
2406
2416
/**
2407
2417
* @doc method
2408
2418
* @id DS.async_methods:refresh
@@ -2444,7 +2454,122 @@ module.exports = {
2444
2454
updateAll : require ( './updateAll' )
2445
2455
} ;
2446
2456
2447
- } , { "./create" :32 , "./destroy" :33 , "./destroyAll" :34 , "./find" :35 , "./findAll" :36 , "./refresh" :38 , "./save" :39 , "./update" :40 , "./updateAll" :41 } ] , 38 :[ function ( require , module , exports ) {
2457
+ } , { "./create" :32 , "./destroy" :33 , "./destroyAll" :34 , "./find" :35 , "./findAll" :36 , "./loadRelations" :38 , "./refresh" :39 , "./save" :40 , "./update" :41 , "./updateAll" :42 } ] , 38 :[ function ( require , module , exports ) {
2458
+ var errorPrefix = 'DS.loadRelations(resourceName, instance(Id), relations[, options]): ' ;
2459
+
2460
+ /**
2461
+ * @doc method
2462
+ * @id DS.async_methods:loadRelations
2463
+ * @name loadRelations
2464
+ * @description
2465
+ * Asynchronously load the indicates relations of the given instance.
2466
+ *
2467
+ * ## Signature:
2468
+ * ```js
2469
+ * DS.loadRelations(resourceName, instance(Id), relations[, options])
2470
+ * ```
2471
+ *
2472
+ * ## Example:
2473
+ *
2474
+ * ```js
2475
+ * ```
2476
+ *
2477
+ * @param {string } resourceName The resource type, e.g. 'user', 'comment', etc.
2478
+ * @param {string|number|object } instance The instance or the id of the instance for which relations are to be loaded.
2479
+ * @param {string|array= } relations The relation(s) to load.
2480
+ * @param {object= } options Optional configuration that is passed to the `find` and `findAll` methods that may be called.
2481
+ *
2482
+ * @returns {Promise } Promise produced by the `$q` service.
2483
+ *
2484
+ * ## Resolves with:
2485
+ *
2486
+ * - `{object}` - `item` - The instance with its loaded relations.
2487
+ *
2488
+ * ## Rejects with:
2489
+ *
2490
+ * - `{IllegalArgumentError}`
2491
+ * - `{NonexistentResourceError}`
2492
+ */
2493
+ function loadRelations ( resourceName , instance , relations , options ) {
2494
+ var deferred = this . $q . defer ( ) ;
2495
+ var promise = deferred . promise ;
2496
+ var IA = this . errors . IA ;
2497
+
2498
+ options = options || { } ;
2499
+
2500
+ if ( angular . isString ( instance ) || angular . isNumber ( instance ) ) {
2501
+ instance = this . get ( resourceName , instance ) ;
2502
+ }
2503
+
2504
+ if ( angular . isString ( relations ) ) {
2505
+ relations = [ relations ] ;
2506
+ }
2507
+
2508
+ try {
2509
+ if ( ! this . definitions [ resourceName ] ) {
2510
+ throw new this . errors . NER ( errorPrefix + resourceName ) ;
2511
+ } else if ( ! this . utils . isObject ( instance ) ) {
2512
+ throw new IA ( errorPrefix + 'instance(Id): Must be a string, number or object!' ) ;
2513
+ } else if ( ! this . utils . isArray ( relations ) ) {
2514
+ throw new IA ( errorPrefix + 'relations: Must be a string or an array!' ) ;
2515
+ } else if ( ! this . utils . isObject ( options ) ) {
2516
+ throw new IA ( errorPrefix + 'options: Must be an object!' ) ;
2517
+ }
2518
+
2519
+ var definition = this . definitions [ resourceName ] ;
2520
+ var _this = this ;
2521
+ var tasks = [ ] ;
2522
+ var fields = [ ] ;
2523
+
2524
+ _this . utils . forOwn ( definition . relations , function ( relation , type ) {
2525
+ _this . utils . forOwn ( relation , function ( def , relationName ) {
2526
+ if ( _this . utils . contains ( relations , relationName ) ) {
2527
+ var task ;
2528
+ var params = { } ;
2529
+ params [ def . foreignKey ] = instance [ definition . idAttribute ] ;
2530
+
2531
+ if ( type === 'hasMany' ) {
2532
+ task = _this . findAll ( relationName , params , options ) ;
2533
+ } else if ( type === 'hasOne' ) {
2534
+ if ( def . localKey && instance [ def . localKey ] ) {
2535
+ task = _this . find ( relationName , instance [ def . localKey ] , options ) ;
2536
+ } else if ( def . foreignKey ) {
2537
+ task = _this . findAll ( relationName , params , options ) ;
2538
+ }
2539
+ } else {
2540
+ task = _this . find ( relationName , instance [ def . localKey ] , options ) ;
2541
+ }
2542
+
2543
+ if ( task ) {
2544
+ tasks . push ( task ) ;
2545
+ fields . push ( def . localField ) ;
2546
+ }
2547
+ }
2548
+ } ) ;
2549
+ } ) ;
2550
+
2551
+ promise = promise
2552
+ . then ( function ( ) {
2553
+ return _this . $q . all ( tasks ) ;
2554
+ } )
2555
+ . then ( function ( loadedRelations ) {
2556
+ angular . forEach ( fields , function ( field , index ) {
2557
+ instance [ field ] = loadedRelations [ index ] ;
2558
+ } ) ;
2559
+ return instance ;
2560
+ } ) ;
2561
+
2562
+ deferred . resolve ( ) ;
2563
+ } catch ( err ) {
2564
+ deferred . reject ( err ) ;
2565
+ }
2566
+
2567
+ return promise ;
2568
+ }
2569
+
2570
+ module . exports = loadRelations ;
2571
+
2572
+ } , { } ] , 39 :[ function ( require , module , exports ) {
2448
2573
var errorPrefix = 'DS.refresh(resourceName, id[, options]): ' ;
2449
2574
2450
2575
/**
@@ -2517,7 +2642,7 @@ function refresh(resourceName, id, options) {
2517
2642
2518
2643
module . exports = refresh ;
2519
2644
2520
- } , { } ] , 39 :[ function ( require , module , exports ) {
2645
+ } , { } ] , 40 :[ function ( require , module , exports ) {
2521
2646
var errorPrefix = 'DS.save(resourceName, id[, options]): ' ;
2522
2647
2523
2648
/**
@@ -2636,7 +2761,7 @@ function save(resourceName, id, options) {
2636
2761
2637
2762
module . exports = save ;
2638
2763
2639
- } , { } ] , 40 :[ function ( require , module , exports ) {
2764
+ } , { } ] , 41 :[ function ( require , module , exports ) {
2640
2765
var errorPrefix = 'DS.update(resourceName, id, attrs[, options]): ' ;
2641
2766
2642
2767
/**
@@ -2746,7 +2871,7 @@ function update(resourceName, id, attrs, options) {
2746
2871
2747
2872
module . exports = update ;
2748
2873
2749
- } , { } ] , 41 :[ function ( require , module , exports ) {
2874
+ } , { } ] , 42 :[ function ( require , module , exports ) {
2750
2875
var errorPrefix = 'DS.updateAll(resourceName, attrs, params[, options]): ' ;
2751
2876
2752
2877
/**
@@ -2865,7 +2990,7 @@ function updateAll(resourceName, attrs, params, options) {
2865
2990
2866
2991
module . exports = updateAll ;
2867
2992
2868
- } , { } ] , 42 :[ function ( require , module , exports ) {
2993
+ } , { } ] , 43 :[ function ( require , module , exports ) {
2869
2994
var utils = require ( '../utils' ) [ 0 ] ( ) ;
2870
2995
2871
2996
function lifecycleNoop ( resourceName , attrs , cb ) {
@@ -3590,7 +3715,7 @@ function DSProvider() {
3590
3715
3591
3716
module . exports = DSProvider ;
3592
3717
3593
- } , { "../utils" :60 , "./async_methods" :37 , "./sync_methods" :53 } ] , 43 :[ function ( require , module , exports ) {
3718
+ } , { "../utils" :61 , "./async_methods" :37 , "./sync_methods" :54 } ] , 44 :[ function ( require , module , exports ) {
3594
3719
var errorPrefix = 'DS.bindAll(scope, expr, resourceName, params[, cb]): ' ;
3595
3720
3596
3721
/**
@@ -3670,7 +3795,7 @@ function bindOne(scope, expr, resourceName, params, cb) {
3670
3795
3671
3796
module . exports = bindOne ;
3672
3797
3673
- } , { } ] , 44 :[ function ( require , module , exports ) {
3798
+ } , { } ] , 45 :[ function ( require , module , exports ) {
3674
3799
var errorPrefix = 'DS.bindOne(scope, expr, resourceName, id[, cb]): ' ;
3675
3800
3676
3801
/**
@@ -3738,7 +3863,7 @@ function bindOne(scope, expr, resourceName, id, cb) {
3738
3863
3739
3864
module . exports = bindOne ;
3740
3865
3741
- } , { } ] , 45 :[ function ( require , module , exports ) {
3866
+ } , { } ] , 46 :[ function ( require , module , exports ) {
3742
3867
var errorPrefix = 'DS.changes(resourceName, id): ' ;
3743
3868
3744
3869
/**
@@ -3790,7 +3915,7 @@ function changes(resourceName, id) {
3790
3915
3791
3916
module . exports = changes ;
3792
3917
3793
- } , { } ] , 46 :[ function ( require , module , exports ) {
3918
+ } , { } ] , 47 :[ function ( require , module , exports ) {
3794
3919
/*jshint evil:true*/
3795
3920
var errorPrefix = 'DS.defineResource(definition): ' ;
3796
3921
@@ -3931,7 +4056,7 @@ function defineResource(definition) {
3931
4056
3932
4057
module . exports = defineResource ;
3933
4058
3934
- } , { } ] , 47 :[ function ( require , module , exports ) {
4059
+ } , { } ] , 48 :[ function ( require , module , exports ) {
3935
4060
var observe = require ( '../../../lib/observe-js/observe-js' ) ;
3936
4061
3937
4062
/**
@@ -3966,7 +4091,7 @@ function digest() {
3966
4091
3967
4092
module . exports = digest ;
3968
4093
3969
- } , { "../../../lib/observe-js/observe-js" :1 } ] , 48 :[ function ( require , module , exports ) {
4094
+ } , { "../../../lib/observe-js/observe-js" :1 } ] , 49 :[ function ( require , module , exports ) {
3970
4095
var errorPrefix = 'DS.eject(resourceName, id): ' ;
3971
4096
3972
4097
function _eject ( definition , resource , id ) {
@@ -4044,7 +4169,7 @@ function eject(resourceName, id) {
4044
4169
4045
4170
module . exports = eject ;
4046
4171
4047
- } , { } ] , 49 :[ function ( require , module , exports ) {
4172
+ } , { } ] , 50 :[ function ( require , module , exports ) {
4048
4173
var errorPrefix = 'DS.ejectAll(resourceName[, params]): ' ;
4049
4174
4050
4175
function _ejectAll ( definition , resource , params ) {
@@ -4152,7 +4277,7 @@ function ejectAll(resourceName, params) {
4152
4277
4153
4278
module . exports = ejectAll ;
4154
4279
4155
- } , { } ] , 50 :[ function ( require , module , exports ) {
4280
+ } , { } ] , 51 :[ function ( require , module , exports ) {
4156
4281
var errorPrefix = 'DS.filter(resourceName[, params][, options]): ' ;
4157
4282
4158
4283
/**
@@ -4231,7 +4356,7 @@ function filter(resourceName, params, options) {
4231
4356
4232
4357
module . exports = filter ;
4233
4358
4234
- } , { } ] , 51 :[ function ( require , module , exports ) {
4359
+ } , { } ] , 52 :[ function ( require , module , exports ) {
4235
4360
var errorPrefix = 'DS.get(resourceName, id[, options]): ' ;
4236
4361
4237
4362
/**
@@ -4290,7 +4415,7 @@ function get(resourceName, id, options) {
4290
4415
4291
4416
module . exports = get ;
4292
4417
4293
- } , { } ] , 52 :[ function ( require , module , exports ) {
4418
+ } , { } ] , 53 :[ function ( require , module , exports ) {
4294
4419
var errorPrefix = 'DS.hasChanges(resourceName, id): ' ;
4295
4420
4296
4421
function diffIsEmpty ( utils , diff ) {
@@ -4348,7 +4473,7 @@ function hasChanges(resourceName, id) {
4348
4473
4349
4474
module . exports = hasChanges ;
4350
4475
4351
- } , { } ] , 53 :[ function ( require , module , exports ) {
4476
+ } , { } ] , 54 :[ function ( require , module , exports ) {
4352
4477
module . exports = {
4353
4478
/**
4354
4479
* @doc method
@@ -4491,7 +4616,7 @@ module.exports = {
4491
4616
hasChanges : require ( './hasChanges' )
4492
4617
} ;
4493
4618
4494
- } , { "./bindAll" :43 , "./bindOne" :44 , "./changes" :45 , "./defineResource" :46 , "./digest" :47 , "./eject" :48 , "./ejectAll" :49 , "./filter" :50 , "./get" :51 , "./hasChanges" :52 , "./inject" :54 , "./lastModified" :55 , "./lastSaved" :56 , "./previous" :57 } ] , 54 :[ function ( require , module , exports ) {
4619
+ } , { "./bindAll" :44 , "./bindOne" :45 , "./changes" :46 , "./defineResource" :47 , "./digest" :48 , "./eject" :49 , "./ejectAll" :50 , "./filter" :51 , "./get" :52 , "./hasChanges" :53 , "./inject" :55 , "./lastModified" :56 , "./lastSaved" :57 , "./previous" :58 } ] , 55 :[ function ( require , module , exports ) {
4495
4620
var observe = require ( '../../../lib/observe-js/observe-js' ) ;
4496
4621
var errorPrefix = 'DS.inject(resourceName, attrs[, options]): ' ;
4497
4622
@@ -4663,7 +4788,7 @@ function inject(resourceName, attrs, options) {
4663
4788
4664
4789
module . exports = inject ;
4665
4790
4666
- } , { "../../../lib/observe-js/observe-js" :1 } ] , 55 :[ function ( require , module , exports ) {
4791
+ } , { "../../../lib/observe-js/observe-js" :1 } ] , 56 :[ function ( require , module , exports ) {
4667
4792
var errorPrefix = 'DS.lastModified(resourceName[, id]): ' ;
4668
4793
4669
4794
/**
@@ -4716,7 +4841,7 @@ function lastModified(resourceName, id) {
4716
4841
4717
4842
module . exports = lastModified ;
4718
4843
4719
- } , { } ] , 56 :[ function ( require , module , exports ) {
4844
+ } , { } ] , 57 :[ function ( require , module , exports ) {
4720
4845
var errorPrefix = 'DS.lastSaved(resourceName[, id]): ' ;
4721
4846
4722
4847
/**
@@ -4772,7 +4897,7 @@ function lastSaved(resourceName, id) {
4772
4897
4773
4898
module . exports = lastSaved ;
4774
4899
4775
- } , { } ] , 57 :[ function ( require , module , exports ) {
4900
+ } , { } ] , 58 :[ function ( require , module , exports ) {
4776
4901
var errorPrefix = 'DS.previous(resourceName, id): ' ;
4777
4902
4778
4903
/**
@@ -4822,7 +4947,7 @@ function previous(resourceName, id) {
4822
4947
4823
4948
module . exports = previous ;
4824
4949
4825
- } , { } ] , 58 :[ function ( require , module , exports ) {
4950
+ } , { } ] , 59 :[ function ( require , module , exports ) {
4826
4951
/**
4827
4952
* @doc function
4828
4953
* @id errors.types:IllegalArgumentError
@@ -4955,7 +5080,7 @@ module.exports = [function () {
4955
5080
} ;
4956
5081
} ] ;
4957
5082
4958
- } , { } ] , 59 :[ function ( require , module , exports ) {
5083
+ } , { } ] , 60 :[ function ( require , module , exports ) {
4959
5084
( function ( window , angular , undefined ) {
4960
5085
'use strict' ;
4961
5086
@@ -5038,7 +5163,7 @@ module.exports = [function () {
5038
5163
5039
5164
} ) ( window , window . angular ) ;
5040
5165
5041
- } , { "./adapters/http" :30 , "./adapters/localStorage" :31 , "./datastore" :42 , "./errors" :58 , "./utils" :60 } ] , 60 :[ function ( require , module , exports ) {
5166
+ } , { "./adapters/http" :30 , "./adapters/localStorage" :31 , "./datastore" :43 , "./errors" :59 , "./utils" :61 } ] , 61 :[ function ( require , module , exports ) {
5042
5167
module . exports = [ function ( ) {
5043
5168
return {
5044
5169
isString : angular . isString ,
@@ -5120,4 +5245,4 @@ module.exports = [function () {
5120
5245
} ;
5121
5246
} ] ;
5122
5247
5123
- } , { "mout/array/contains" :2 , "mout/array/filter" :3 , "mout/array/slice" :7 , "mout/array/sort" :8 , "mout/array/toLookup" :9 , "mout/lang/isEmpty" :14 , "mout/object/deepMixIn" :21 , "mout/object/forOwn" :23 , "mout/object/pick" :26 , "mout/object/set" :27 , "mout/string/makePath" :28 , "mout/string/upperCase" :29 } ] } , { } , [ 59 ] )
5248
+ } , { "mout/array/contains" :2 , "mout/array/filter" :3 , "mout/array/slice" :7 , "mout/array/sort" :8 , "mout/array/toLookup" :9 , "mout/lang/isEmpty" :14 , "mout/object/deepMixIn" :21 , "mout/object/forOwn" :23 , "mout/object/pick" :26 , "mout/object/set" :27 , "mout/string/makePath" :28 , "mout/string/upperCase" :29 } ] } , { } , [ 60 ] )
0 commit comments