@@ -1472,7 +1472,6 @@ function DSHttpAdapterProvider() {
1472
1472
* ```js
1473
1473
* angular.module('myApp').config(function (DSHttpAdapterProvider) {
1474
1474
* angular.extend(DSHttpAdapterProvider.defaults.$httpConfig, {
1475
- * interceptor: [...],
1476
1475
* headers: {
1477
1476
* Authorization: 'Basic YmVlcDpib29w'
1478
1477
* },
@@ -4217,7 +4216,7 @@ function DSProvider() {
4217
4216
4218
4217
module . exports = DSProvider ;
4219
4218
4220
- } , { "../utils" :71 , "./async_methods" :43 , "./sync_methods" :61 } ] , 50 :[ function ( require , module , exports ) {
4219
+ } , { "../utils" :72 , "./async_methods" :43 , "./sync_methods" :62 } ] , 50 :[ function ( require , module , exports ) {
4221
4220
function errorPrefix ( resourceName ) {
4222
4221
return 'DS.bindAll(scope, expr, ' + resourceName + ', params[, cb]): ' ;
4223
4222
}
@@ -4439,6 +4438,102 @@ function changes(resourceName, id) {
4439
4438
module . exports = changes ;
4440
4439
4441
4440
} , { } ] , 53 :[ function ( require , module , exports ) {
4441
+ function errorPrefix ( resourceName ) {
4442
+ return 'DS.compute(' + resourceName + ', instance): ' ;
4443
+ }
4444
+
4445
+ function _compute ( fn , field ) {
4446
+ var _this = this ;
4447
+ var args = [ ] ;
4448
+ angular . forEach ( fn . deps , function ( dep ) {
4449
+ args . push ( _this [ dep ] ) ;
4450
+ } ) ;
4451
+ // compute property
4452
+ this [ field ] = fn [ fn . length - 1 ] . apply ( this , args ) ;
4453
+ }
4454
+
4455
+ /**
4456
+ * @doc method
4457
+ * @id DS.sync methods:compute
4458
+ * @name compute
4459
+ * @description
4460
+ * Force the given instance or the item with the given primary key to recompute its computed properties.
4461
+ *
4462
+ * ## Signature:
4463
+ * ```js
4464
+ * DS.compute(resourceName, instance)
4465
+ * ```
4466
+ *
4467
+ * ## Example:
4468
+ *
4469
+ * ```js
4470
+ * var User = DS.defineResource({
4471
+ * name: 'user',
4472
+ * computed: {
4473
+ * fullName: ['first', 'last', function (first, last) {
4474
+ * return first + ' ' + last;
4475
+ * }]
4476
+ * }
4477
+ * });
4478
+ *
4479
+ * var user = User.createInstance({ first: 'John', last: 'Doe' });
4480
+ * user.fullName; // undefined
4481
+ *
4482
+ * User.compute(user);
4483
+ *
4484
+ * user.fullName; // "John Doe"
4485
+ *
4486
+ * var user2 = User.inject({ id: 2, first: 'Jane', last: 'Doe' });
4487
+ * user2.fullName; // undefined
4488
+ *
4489
+ * User.compute(1);
4490
+ *
4491
+ * user2.fullName; // "Jane Doe"
4492
+ *
4493
+ * // if you don't pass useClass: false then you can do:
4494
+ * var user3 = User.createInstance({ first: 'Sally', last: 'Doe' });
4495
+ * user3.fullName; // undefined
4496
+ * user3.DSCompute();
4497
+ * user3.fullName; // "Sally Doe"
4498
+ * ```
4499
+ *
4500
+ * ## Throws
4501
+ *
4502
+ * - `{IllegalArgumentError}`
4503
+ * - `{NonexistentResourceError}`
4504
+ *
4505
+ * @param {string } resourceName The resource type, e.g. 'user', 'comment', etc.
4506
+ * @param {object|string|number } instance Instance or primary key of the instance (must be in the store) for which to recompute properties.
4507
+ * @returns {Object } The instance.
4508
+ */
4509
+ function compute ( resourceName , instance ) {
4510
+ var DS = this ;
4511
+ var IA = DS . errors . IA ;
4512
+ var definition = DS . definitions [ resourceName ] ;
4513
+
4514
+ if ( ! definition ) {
4515
+ throw new DS . errors . NER ( errorPrefix ( resourceName ) + resourceName ) ;
4516
+ } else if ( ! DS . utils . isObject ( instance ) && ! DS . utils . isString ( instance ) && ! DS . utils . isNumber ( instance ) ) {
4517
+ throw new IA ( errorPrefix ( resourceName ) + 'instance: Must be an object, string or number!' ) ;
4518
+ }
4519
+
4520
+ if ( DS . utils . isString ( instance ) || DS . utils . isNumber ( instance ) ) {
4521
+ instance = DS . get ( resourceName , instance ) ;
4522
+ }
4523
+
4524
+ DS . utils . forOwn ( definition . computed , function ( fn , field ) {
4525
+ _compute . call ( instance , fn , field ) ;
4526
+ } ) ;
4527
+
4528
+ return instance ;
4529
+ }
4530
+
4531
+ module . exports = {
4532
+ compute : compute ,
4533
+ _compute : _compute
4534
+ } ;
4535
+
4536
+ } , { } ] , 54 :[ function ( require , module , exports ) {
4442
4537
function errorPrefix ( resourceName ) {
4443
4538
return 'DS.createInstance(' + resourceName + '[, attrs][, options]): ' ;
4444
4539
}
@@ -4535,7 +4630,7 @@ function createInstance(resourceName, attrs, options) {
4535
4630
4536
4631
module . exports = createInstance ;
4537
4632
4538
- } , { } ] , 54 :[ function ( require , module , exports ) {
4633
+ } , { } ] , 55 :[ function ( require , module , exports ) {
4539
4634
/*jshint evil:true*/
4540
4635
var errorPrefix = 'DS.defineResource(definition): ' ;
4541
4636
@@ -4568,6 +4663,9 @@ var methodsToProxy = [
4568
4663
'inject' ,
4569
4664
'lastModified' ,
4570
4665
'lastSaved' ,
4666
+ 'link' ,
4667
+ 'linkAll' ,
4668
+ 'linkInverse' ,
4571
4669
'loadRelations' ,
4572
4670
'previous' ,
4573
4671
'refresh' ,
@@ -4774,6 +4872,10 @@ function defineResource(definition) {
4774
4872
return ! ! dep ;
4775
4873
} ) ;
4776
4874
} ) ;
4875
+
4876
+ def [ def . class ] . prototype . DSCompute = function ( ) {
4877
+ return DS . compute ( def . name , this ) ;
4878
+ } ;
4777
4879
}
4778
4880
4779
4881
// Initialize store data for the new resource
@@ -4817,7 +4919,7 @@ function defineResource(definition) {
4817
4919
4818
4920
module . exports = defineResource ;
4819
4921
4820
- } , { } ] , 55 :[ function ( require , module , exports ) {
4922
+ } , { } ] , 56 :[ function ( require , module , exports ) {
4821
4923
var observe = require ( '../../../lib/observe-js/observe-js' ) ;
4822
4924
4823
4925
/**
@@ -4853,7 +4955,7 @@ function digest() {
4853
4955
4854
4956
module . exports = digest ;
4855
4957
4856
- } , { "../../../lib/observe-js/observe-js" :1 } ] , 56 :[ function ( require , module , exports ) {
4958
+ } , { "../../../lib/observe-js/observe-js" :1 } ] , 57 :[ function ( require , module , exports ) {
4857
4959
function errorPrefix ( resourceName , id ) {
4858
4960
return 'DS.eject(' + resourceName + ', ' + id + '): ' ;
4859
4961
}
@@ -4946,7 +5048,7 @@ function eject(resourceName, id) {
4946
5048
4947
5049
module . exports = eject ;
4948
5050
4949
- } , { } ] , 57 :[ function ( require , module , exports ) {
5051
+ } , { } ] , 58 :[ function ( require , module , exports ) {
4950
5052
function errorPrefix ( resourceName ) {
4951
5053
return 'DS.ejectAll(' + resourceName + '[, params]): ' ;
4952
5054
}
@@ -5060,7 +5162,7 @@ function ejectAll(resourceName, params) {
5060
5162
5061
5163
module . exports = ejectAll ;
5062
5164
5063
- } , { } ] , 58 :[ function ( require , module , exports ) {
5165
+ } , { } ] , 59 :[ function ( require , module , exports ) {
5064
5166
function errorPrefix ( resourceName ) {
5065
5167
return 'DS.filter(' + resourceName + '[, params][, options]): ' ;
5066
5168
}
@@ -5143,7 +5245,7 @@ function filter(resourceName, params, options) {
5143
5245
5144
5246
module . exports = filter ;
5145
5247
5146
- } , { } ] , 59 :[ function ( require , module , exports ) {
5248
+ } , { } ] , 60 :[ function ( require , module , exports ) {
5147
5249
function errorPrefix ( resourceName , id ) {
5148
5250
return 'DS.get(' + resourceName + ', ' + id + '): ' ;
5149
5251
}
@@ -5207,7 +5309,7 @@ function get(resourceName, id, options) {
5207
5309
5208
5310
module . exports = get ;
5209
5311
5210
- } , { } ] , 60 :[ function ( require , module , exports ) {
5312
+ } , { } ] , 61 :[ function ( require , module , exports ) {
5211
5313
function errorPrefix ( resourceName , id ) {
5212
5314
return 'DS.hasChanges(' + resourceName + ', ' + id + '): ' ;
5213
5315
}
@@ -5270,7 +5372,7 @@ function hasChanges(resourceName, id) {
5270
5372
5271
5373
module . exports = hasChanges ;
5272
5374
5273
- } , { } ] , 61 :[ function ( require , module , exports ) {
5375
+ } , { } ] , 62 :[ function ( require , module , exports ) {
5274
5376
module . exports = {
5275
5377
5276
5378
/**
@@ -5293,6 +5395,16 @@ module.exports = {
5293
5395
*/
5294
5396
bindAll : require ( './bindAll' ) ,
5295
5397
5398
+ /**
5399
+ * @doc method
5400
+ * @id DS.sync methods:compute
5401
+ * @name compute
5402
+ * @methodOf DS
5403
+ * @description
5404
+ * See [DS.compute](/documentation/api/api/DS.sync methods:compute).
5405
+ */
5406
+ compute : require ( './compute' ) . compute ,
5407
+
5296
5408
/**
5297
5409
* @doc method
5298
5410
* @id DS.sync methods:createInstance
@@ -5454,8 +5566,9 @@ module.exports = {
5454
5566
hasChanges : require ( './hasChanges' )
5455
5567
} ;
5456
5568
5457
- } , { "./bindAll" :50 , "./bindOne" :51 , "./changes" :52 , "./createInstance " :53 , "./defineResource " :54 , "./digest " :55 , "./eject " :56 , "./ejectAll " :57 , "./filter " :58 , "./get " :59 , "./hasChanges " :60 , "./inject" :62 , "./lastModified" :63 , "./lastSaved" :64 , "./link" :65 , "./linkAll" :66 , "./linkInverse" :67 , "./previous" :68 } ] , 62 :[ function ( require , module , exports ) {
5569
+ } , { "./bindAll" :50 , "./bindOne" :51 , "./changes" :52 , "./compute " :53 , "./createInstance " :54 , "./defineResource " :55 , "./digest " :56 , "./eject " :57 , "./ejectAll " :58 , "./filter " :59 , "./get " :60 , "./hasChanges" : 61 , "./ inject" :63 , "./lastModified" :64 , "./lastSaved" :65 , "./link" :66 , "./linkAll" :67 , "./linkInverse" :68 , "./previous" :69 } ] , 63 :[ function ( require , module , exports ) {
5458
5570
var observe = require ( '../../../lib/observe-js/observe-js' ) ;
5571
+ var _compute = require ( './compute' ) . _compute ;
5459
5572
var stack = 0 ;
5460
5573
var data = {
5461
5574
injectedSoFar : { }
@@ -5489,12 +5602,7 @@ function _inject(definition, resource, attrs) {
5489
5602
} ) ;
5490
5603
compute = compute || ! fn . deps . length ;
5491
5604
if ( compute ) {
5492
- var args = [ ] ;
5493
- angular . forEach ( fn . deps , function ( dep ) {
5494
- args . push ( item [ dep ] ) ;
5495
- } ) ;
5496
- // recompute property
5497
- item [ field ] = fn [ fn . length - 1 ] . apply ( item , args ) ;
5605
+ _compute . call ( item , fn , field ) ;
5498
5606
}
5499
5607
} ) ;
5500
5608
}
@@ -5758,7 +5866,7 @@ function inject(resourceName, attrs, options) {
5758
5866
5759
5867
module . exports = inject ;
5760
5868
5761
- } , { "../../../lib/observe-js/observe-js" :1 } ] , 63 :[ function ( require , module , exports ) {
5869
+ } , { "../../../lib/observe-js/observe-js" :1 , "./compute" : 53 } ] , 64 :[ function ( require , module , exports ) {
5762
5870
function errorPrefix ( resourceName , id ) {
5763
5871
return 'DS.lastModified(' + resourceName + '[, ' + id + ']): ' ;
5764
5872
}
@@ -5815,7 +5923,7 @@ function lastModified(resourceName, id) {
5815
5923
5816
5924
module . exports = lastModified ;
5817
5925
5818
- } , { } ] , 64 :[ function ( require , module , exports ) {
5926
+ } , { } ] , 65 :[ function ( require , module , exports ) {
5819
5927
function errorPrefix ( resourceName , id ) {
5820
5928
return 'DS.lastSaved(' + resourceName + '[, ' + id + ']): ' ;
5821
5929
}
@@ -5877,7 +5985,7 @@ function lastSaved(resourceName, id) {
5877
5985
5878
5986
module . exports = lastSaved ;
5879
5987
5880
- } , { } ] , 65 :[ function ( require , module , exports ) {
5988
+ } , { } ] , 66 :[ function ( require , module , exports ) {
5881
5989
function errorPrefix ( resourceName ) {
5882
5990
return 'DS.link(' + resourceName + ', id[, relations]): ' ;
5883
5991
}
@@ -5985,7 +6093,7 @@ function link(resourceName, id, relations) {
5985
6093
5986
6094
module . exports = link ;
5987
6095
5988
- } , { } ] , 66 :[ function ( require , module , exports ) {
6096
+ } , { } ] , 67 :[ function ( require , module , exports ) {
5989
6097
function errorPrefix ( resourceName ) {
5990
6098
return 'DS.linkAll(' + resourceName + '[, params][, relations]): ' ;
5991
6099
}
@@ -6109,7 +6217,7 @@ function linkAll(resourceName, params, relations) {
6109
6217
6110
6218
module . exports = linkAll ;
6111
6219
6112
- } , { } ] , 67 :[ function ( require , module , exports ) {
6220
+ } , { } ] , 68 :[ function ( require , module , exports ) {
6113
6221
function errorPrefix ( resourceName ) {
6114
6222
return 'DS.linkInverse(' + resourceName + ', id[, relations]): ' ;
6115
6223
}
@@ -6205,7 +6313,7 @@ function linkInverse(resourceName, id, relations) {
6205
6313
6206
6314
module . exports = linkInverse ;
6207
6315
6208
- } , { } ] , 68 :[ function ( require , module , exports ) {
6316
+ } , { } ] , 69 :[ function ( require , module , exports ) {
6209
6317
function errorPrefix ( resourceName , id ) {
6210
6318
return 'DS.previous(' + resourceName + '[, ' + id + ']): ' ;
6211
6319
}
@@ -6260,7 +6368,7 @@ function previous(resourceName, id) {
6260
6368
6261
6369
module . exports = previous ;
6262
6370
6263
- } , { } ] , 69 :[ function ( require , module , exports ) {
6371
+ } , { } ] , 70 :[ function ( require , module , exports ) {
6264
6372
/**
6265
6373
* @doc function
6266
6374
* @id errors.types:IllegalArgumentError
@@ -6393,7 +6501,7 @@ module.exports = [function () {
6393
6501
} ;
6394
6502
} ] ;
6395
6503
6396
- } , { } ] , 70 :[ function ( require , module , exports ) {
6504
+ } , { } ] , 71 :[ function ( require , module , exports ) {
6397
6505
( function ( window , angular , undefined ) {
6398
6506
'use strict' ;
6399
6507
@@ -6476,7 +6584,7 @@ module.exports = [function () {
6476
6584
6477
6585
} ) ( window , window . angular ) ;
6478
6586
6479
- } , { "./adapters/http" :36 , "./adapters/localStorage" :37 , "./datastore" :49 , "./errors" :69 , "./utils" :71 } ] , 71 :[ function ( require , module , exports ) {
6587
+ } , { "./adapters/http" :36 , "./adapters/localStorage" :37 , "./datastore" :49 , "./errors" :70 , "./utils" :72 } ] , 72 :[ function ( require , module , exports ) {
6480
6588
module . exports = [ function ( ) {
6481
6589
return {
6482
6590
isBoolean : require ( 'mout/lang/isBoolean' ) ,
@@ -6561,4 +6669,4 @@ module.exports = [function () {
6561
6669
} ;
6562
6670
} ] ;
6563
6671
6564
- } , { "mout/array/contains" :2 , "mout/array/filter" :3 , "mout/array/slice" :7 , "mout/array/sort" :8 , "mout/array/toLookup" :9 , "mout/lang/isBoolean" :14 , "mout/lang/isEmpty" :15 , "mout/object/deepMixIn" :22 , "mout/object/forOwn" :24 , "mout/object/pick" :27 , "mout/object/set" :28 , "mout/string/makePath" :31 , "mout/string/pascalCase" :32 , "mout/string/upperCase" :35 } ] } , { } , [ 70 ] ) ;
6672
+ } , { "mout/array/contains" :2 , "mout/array/filter" :3 , "mout/array/slice" :7 , "mout/array/sort" :8 , "mout/array/toLookup" :9 , "mout/lang/isBoolean" :14 , "mout/lang/isEmpty" :15 , "mout/object/deepMixIn" :22 , "mout/object/forOwn" :24 , "mout/object/pick" :27 , "mout/object/set" :28 , "mout/string/makePath" :31 , "mout/string/pascalCase" :32 , "mout/string/upperCase" :35 } ] } , { } , [ 71 ] ) ;
0 commit comments