1
1
/**
2
2
* @author Jason Dobry <[email protected] >
3
3
* @file angular-data.js
4
- * @version 0.9.0-SNAPSHOT - Homepage <http://angular-data.codetrain.io/>
4
+ * @version 0.9.0 - Homepage <http://angular-data.codetrain.io/>
5
5
* @copyright (c) 2014 Jason Dobry <https://github.com/jmdobry/>
6
6
* @license MIT <https://github.com/jmdobry/angular-data/blob/master/LICENSE>
7
7
*
@@ -2250,10 +2250,20 @@ module.exports = {
2250
2250
* @description
2251
2251
* See [DS.save](/documentation/api/api/DS.async_methods:save).
2252
2252
*/
2253
- save : require ( './save' )
2253
+ save : require ( './save' ) ,
2254
+
2255
+ /**
2256
+ * @doc method
2257
+ * @id DS.async_methods:update
2258
+ * @name update
2259
+ * @methodOf DS
2260
+ * @description
2261
+ * See [DS.update](/documentation/api/api/DS.async_methods:update).
2262
+ */
2263
+ update : require ( './update' )
2254
2264
} ;
2255
2265
2256
- } , { "./create" :32 , "./destroy" :33 , "./destroyAll" :34 , "./find" :35 , "./findAll" :36 , "./refresh" :38 , "./save" :39 } ] , 38 :[ function ( require , module , exports ) {
2266
+ } , { "./create" :32 , "./destroy" :33 , "./destroyAll" :34 , "./find" :35 , "./findAll" :36 , "./refresh" :38 , "./save" :39 , "./update" : 40 } ] , 38 :[ function ( require , module , exports ) {
2257
2267
var errorPrefix = 'DS.refresh(resourceName, id[, options]): ' ;
2258
2268
2259
2269
/**
@@ -2447,6 +2457,116 @@ function save(resourceName, id, options) {
2447
2457
module . exports = save ;
2448
2458
2449
2459
} , { } ] , 40 :[ function ( require , module , exports ) {
2460
+ var errorPrefix = 'DS.update(resourceName, id, attrs[, options]): ' ;
2461
+
2462
+ /**
2463
+ * @doc method
2464
+ * @id DS.async_methods:update
2465
+ * @name update
2466
+ * @description
2467
+ * Update the item of type `resourceName` and primary key `id` with `attrs`. This is useful when you want to update an
2468
+ * item that isn't already in the data store, or you don't want to update the item that's in the data store until the
2469
+ * server-side operation succeeds. This differs from `DS.save` which simply saves items in their current form that
2470
+ * already reside in the data store.
2471
+ *
2472
+ * ## Signature:
2473
+ * ```js
2474
+ * DS.update(resourceName, id, attrs[, options])
2475
+ * ```
2476
+ *
2477
+ * ## Example:
2478
+ *
2479
+ * ```js
2480
+ * DS.get('document', 5); // undefined
2481
+ *
2482
+ * DS.update('document', 5, { title: 'How to cook in style' })
2483
+ * .then(function (document) {
2484
+ * document; // A reference to the document that's been saved to the server
2485
+ * // and now resides in the data store
2486
+ * });
2487
+ * ```
2488
+ *
2489
+ * @param {string } resourceName The resource type, e.g. 'user', 'comment', etc.
2490
+ * @param {string|number } id The primary key of the item to update.
2491
+ * @param {object } attrs The attributes with which to update the item.
2492
+ * @param {object= } options Optional configuration. Properties:
2493
+ * - `{boolean=}` - `cacheResponse` - Inject the data returned by the server into the data store. Default: `true`.
2494
+ *
2495
+ * @returns {Promise } Promise produced by the `$q` service.
2496
+ *
2497
+ * ## Resolves with:
2498
+ *
2499
+ * - `{object}` - `item` - A reference to the newly saved item.
2500
+ *
2501
+ * ## Rejects with:
2502
+ *
2503
+ * - `{IllegalArgumentError}`
2504
+ * - `{RuntimeError}`
2505
+ * - `{UnhandledError}`
2506
+ */
2507
+ function save ( resourceName , id , attrs , options ) {
2508
+ var deferred = this . $q . defer ( ) ,
2509
+ promise = deferred . promise ;
2510
+
2511
+ options = options || { } ;
2512
+
2513
+ if ( ! this . definitions [ resourceName ] ) {
2514
+ deferred . reject ( new this . errors . RuntimeError ( errorPrefix + resourceName + ' is not a registered resource!' ) ) ;
2515
+ } else if ( ! this . utils . isString ( id ) && ! this . utils . isNumber ( id ) ) {
2516
+ deferred . reject ( new this . errors . IllegalArgumentError ( errorPrefix + 'id: Must be a string or a number!' ) ) ;
2517
+ } else if ( ! this . utils . isObject ( attrs ) ) {
2518
+ deferred . reject ( new this . errors . IllegalArgumentError ( errorPrefix + 'attrs: Must be an object!' ) ) ;
2519
+ } else if ( ! this . utils . isObject ( options ) ) {
2520
+ deferred . reject ( new this . errors . IllegalArgumentError ( errorPrefix + 'options: Must be an object!' ) ) ;
2521
+ } else {
2522
+ var definition = this . definitions [ resourceName ] ,
2523
+ resource = this . store [ resourceName ] ,
2524
+ _this = this ;
2525
+
2526
+ if ( ! ( 'cacheResponse' in options ) ) {
2527
+ options . cacheResponse = true ;
2528
+ } else {
2529
+ options . cacheResponse = ! ! options . cacheResponse ;
2530
+ }
2531
+
2532
+ promise = promise
2533
+ . then ( function ( attrs ) {
2534
+ return _this . $q . promisify ( definition . beforeValidate ) ( resourceName , attrs ) ;
2535
+ } )
2536
+ . then ( function ( attrs ) {
2537
+ return _this . $q . promisify ( definition . validate ) ( resourceName , attrs ) ;
2538
+ } )
2539
+ . then ( function ( attrs ) {
2540
+ return _this . $q . promisify ( definition . afterValidate ) ( resourceName , attrs ) ;
2541
+ } )
2542
+ . then ( function ( attrs ) {
2543
+ return _this . $q . promisify ( definition . beforeUpdate ) ( resourceName , attrs ) ;
2544
+ } )
2545
+ . then ( function ( attrs ) {
2546
+ return _this . adapters [ options . adapter || definition . defaultAdapter ] . update ( definition , id , attrs , options ) ;
2547
+ } )
2548
+ . then ( function ( data ) {
2549
+ return _this . $q . promisify ( definition . afterUpdate ) ( resourceName , data ) ;
2550
+ } )
2551
+ . then ( function ( data ) {
2552
+ if ( options . cacheResponse ) {
2553
+ _this . inject ( definition . name , data , options ) ;
2554
+ resource . previousAttributes [ id ] = _this . utils . deepMixIn ( { } , data ) ;
2555
+ resource . saved [ id ] = _this . utils . updateTimestamp ( resource . saved [ id ] ) ;
2556
+ return _this . get ( resourceName , id ) ;
2557
+ } else {
2558
+ return data ;
2559
+ }
2560
+ } ) ;
2561
+
2562
+ deferred . resolve ( attrs ) ;
2563
+ }
2564
+ return promise ;
2565
+ }
2566
+
2567
+ module . exports = save ;
2568
+
2569
+ } , { } ] , 41 :[ function ( require , module , exports ) {
2450
2570
var utils = require ( '../utils' ) [ 0 ] ( ) ;
2451
2571
2452
2572
function lifecycleNoop ( resourceName , attrs , cb ) {
@@ -2656,7 +2776,7 @@ function DSProvider() {
2656
2776
2657
2777
module . exports = DSProvider ;
2658
2778
2659
- } , { "../utils" :"K0yknU" , "./async_methods" :37 , "./sync_methods" :51 } ] , 41 :[ function ( require , module , exports ) {
2779
+ } , { "../utils" :"K0yknU" , "./async_methods" :37 , "./sync_methods" :52 } ] , 42 :[ function ( require , module , exports ) {
2660
2780
var errorPrefix = 'DS.bindAll(scope, expr, resourceName, params): ' ;
2661
2781
2662
2782
/**
@@ -2728,7 +2848,7 @@ function bindOne(scope, expr, resourceName, params) {
2728
2848
2729
2849
module . exports = bindOne ;
2730
2850
2731
- } , { } ] , 42 :[ function ( require , module , exports ) {
2851
+ } , { } ] , 43 :[ function ( require , module , exports ) {
2732
2852
var errorPrefix = 'DS.bindOne(scope, expr, resourceName, id): ' ;
2733
2853
2734
2854
/**
@@ -2788,7 +2908,7 @@ function bindOne(scope, expr, resourceName, id) {
2788
2908
2789
2909
module . exports = bindOne ;
2790
2910
2791
- } , { } ] , 43 :[ function ( require , module , exports ) {
2911
+ } , { } ] , 44 :[ function ( require , module , exports ) {
2792
2912
var errorPrefix = 'DS.changes(resourceName, id): ' ;
2793
2913
2794
2914
/**
@@ -2845,7 +2965,7 @@ function changes(resourceName, id) {
2845
2965
2846
2966
module . exports = changes ;
2847
2967
2848
- } , { } ] , 44 :[ function ( require , module , exports ) {
2968
+ } , { } ] , 45 :[ function ( require , module , exports ) {
2849
2969
/*jshint evil:true*/
2850
2970
var errorPrefix = 'DS.defineResource(definition): ' ;
2851
2971
@@ -2982,7 +3102,7 @@ function defineResource(definition) {
2982
3102
2983
3103
module . exports = defineResource ;
2984
3104
2985
- } , { } ] , 45 :[ function ( require , module , exports ) {
3105
+ } , { } ] , 46 :[ function ( require , module , exports ) {
2986
3106
var observe = require ( 'observejs' ) ;
2987
3107
2988
3108
/**
@@ -3024,7 +3144,7 @@ function digest() {
3024
3144
3025
3145
module . exports = digest ;
3026
3146
3027
- } , { "observejs" :"QYwGEY" } ] , 46 :[ function ( require , module , exports ) {
3147
+ } , { "observejs" :"QYwGEY" } ] , 47 :[ function ( require , module , exports ) {
3028
3148
var errorPrefix = 'DS.eject(resourceName, id): ' ;
3029
3149
3030
3150
function _eject ( definition , resource , id ) {
@@ -3107,7 +3227,7 @@ function eject(resourceName, id) {
3107
3227
3108
3228
module . exports = eject ;
3109
3229
3110
- } , { } ] , 47 :[ function ( require , module , exports ) {
3230
+ } , { } ] , 48 :[ function ( require , module , exports ) {
3111
3231
var errorPrefix = 'DS.ejectAll(resourceName[, params]): ' ;
3112
3232
3113
3233
function _ejectAll ( definition , resource , params ) {
@@ -3211,7 +3331,7 @@ function ejectAll(resourceName, params) {
3211
3331
3212
3332
module . exports = ejectAll ;
3213
3333
3214
- } , { } ] , 48 :[ function ( require , module , exports ) {
3334
+ } , { } ] , 49 :[ function ( require , module , exports ) {
3215
3335
/* jshint loopfunc: true */
3216
3336
var errorPrefix = 'DS.filter(resourceName, params[, options]): ' ;
3217
3337
@@ -3368,7 +3488,7 @@ function filter(resourceName, params, options) {
3368
3488
3369
3489
module . exports = filter ;
3370
3490
3371
- } , { } ] , 49 :[ function ( require , module , exports ) {
3491
+ } , { } ] , 50 :[ function ( require , module , exports ) {
3372
3492
var errorPrefix = 'DS.get(resourceName, id[, options]): ' ;
3373
3493
3374
3494
/**
@@ -3431,7 +3551,7 @@ function get(resourceName, id, options) {
3431
3551
3432
3552
module . exports = get ;
3433
3553
3434
- } , { } ] , 50 :[ function ( require , module , exports ) {
3554
+ } , { } ] , 51 :[ function ( require , module , exports ) {
3435
3555
var errorPrefix = 'DS.hasChanges(resourceName, id): ' ;
3436
3556
3437
3557
function diffIsEmpty ( utils , diff ) {
@@ -3494,7 +3614,7 @@ function hasChanges(resourceName, id) {
3494
3614
3495
3615
module . exports = hasChanges ;
3496
3616
3497
- } , { } ] , 51 :[ function ( require , module , exports ) {
3617
+ } , { } ] , 52 :[ function ( require , module , exports ) {
3498
3618
module . exports = {
3499
3619
/**
3500
3620
* @doc method
@@ -3637,7 +3757,7 @@ module.exports = {
3637
3757
hasChanges : require ( './hasChanges' )
3638
3758
} ;
3639
3759
3640
- } , { "./bindAll" :41 , "./bindOne" :42 , "./changes" :43 , "./defineResource" :44 , "./digest" :45 , "./eject" :46 , "./ejectAll" :47 , "./filter" :48 , "./get" :49 , "./hasChanges" :50 , "./inject" :52 , "./lastModified" :53 , "./lastSaved" :54 , "./previous" :55 } ] , 52 :[ function ( require , module , exports ) {
3760
+ } , { "./bindAll" :42 , "./bindOne" :43 , "./changes" :44 , "./defineResource" :45 , "./digest" :46 , "./eject" :47 , "./ejectAll" :48 , "./filter" :49 , "./get" :50 , "./hasChanges" :51 , "./inject" :53 , "./lastModified" :54 , "./lastSaved" :55 , "./previous" :56 } ] , 53 :[ function ( require , module , exports ) {
3641
3761
var observe = require ( 'observejs' ) ,
3642
3762
errorPrefix = 'DS.inject(resourceName, attrs[, options]): ' ;
3643
3763
@@ -3787,7 +3907,7 @@ function inject(resourceName, attrs, options) {
3787
3907
3788
3908
module . exports = inject ;
3789
3909
3790
- } , { "observejs" :"QYwGEY" } ] , 53 :[ function ( require , module , exports ) {
3910
+ } , { "observejs" :"QYwGEY" } ] , 54 :[ function ( require , module , exports ) {
3791
3911
var errorPrefix = 'DS.lastModified(resourceName[, id]): ' ;
3792
3912
3793
3913
/**
@@ -3845,7 +3965,7 @@ function lastModified(resourceName, id) {
3845
3965
3846
3966
module . exports = lastModified ;
3847
3967
3848
- } , { } ] , 54 :[ function ( require , module , exports ) {
3968
+ } , { } ] , 55 :[ function ( require , module , exports ) {
3849
3969
var errorPrefix = 'DS.lastSaved(resourceName[, id]): ' ;
3850
3970
3851
3971
/**
@@ -3906,7 +4026,7 @@ function lastSaved(resourceName, id) {
3906
4026
3907
4027
module . exports = lastSaved ;
3908
4028
3909
- } , { } ] , 55 :[ function ( require , module , exports ) {
4029
+ } , { } ] , 56 :[ function ( require , module , exports ) {
3910
4030
var errorPrefix = 'DS.previous(resourceName, id): ' ;
3911
4031
3912
4032
/**
@@ -4134,7 +4254,7 @@ module.exports = [function () {
4134
4254
} ;
4135
4255
} ] ;
4136
4256
4137
- } , { } ] , 58 :[ function ( require , module , exports ) {
4257
+ } , { } ] , 59 :[ function ( require , module , exports ) {
4138
4258
( function ( window , angular , undefined ) {
4139
4259
'use strict' ;
4140
4260
@@ -4143,7 +4263,7 @@ module.exports = [function () {
4143
4263
* @id angular-data
4144
4264
* @name angular-data
4145
4265
* @description
4146
- * __Version:__ 0.9.0-SNAPSHOT
4266
+ * __Version:__ 0.9.0
4147
4267
*
4148
4268
* ## Install
4149
4269
*
@@ -4216,7 +4336,7 @@ module.exports = [function () {
4216
4336
4217
4337
} ) ( window , window . angular ) ;
4218
4338
4219
- } , { "./adapters/http" :31 , "./datastore" :40 , "./errors" :"XIsZmp" , "./utils" :"K0yknU" } ] , "K0yknU" :[ function ( require , module , exports ) {
4339
+ } , { "./adapters/http" :31 , "./datastore" :41 , "./errors" :"XIsZmp" , "./utils" :"K0yknU" } ] , "K0yknU" :[ function ( require , module , exports ) {
4220
4340
module . exports = [ function ( ) {
4221
4341
return {
4222
4342
isString : angular . isString ,
@@ -4300,4 +4420,4 @@ module.exports = [function () {
4300
4420
4301
4421
} , { "mout/array/contains" :3 , "mout/array/filter" :4 , "mout/array/slice" :8 , "mout/array/sort" :9 , "mout/array/toLookup" :10 , "mout/lang/isEmpty" :15 , "mout/object/deepMixIn" :22 , "mout/object/forOwn" :24 , "mout/object/pick" :27 , "mout/object/set" :28 , "mout/string/makePath" :29 , "mout/string/upperCase" :30 } ] , "utils" :[ function ( require , module , exports ) {
4302
4422
module . exports = require ( 'K0yknU' ) ;
4303
- } , { } ] } , { } , [ 58 ] )
4423
+ } , { } ] } , { } , [ 59 ] )
0 commit comments