11/**
22 * @author Jason Dobry <jason.dobry@gmail.com>
33 * @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/>
55 * @copyright (c) 2014 Jason Dobry <https://github.com/jmdobry/>
66 * @license MIT <https://github.com/jmdobry/angular-data/blob/master/LICENSE>
77 *
@@ -2250,10 +2250,20 @@ module.exports = {
22502250 * @description
22512251 * See [DS.save](/documentation/api/api/DS.async_methods:save).
22522252 */
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' )
22542264} ;
22552265
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 ) {
22572267var errorPrefix = 'DS.refresh(resourceName, id[, options]): ' ;
22582268
22592269/**
@@ -2447,6 +2457,116 @@ function save(resourceName, id, options) {
24472457module . exports = save ;
24482458
24492459} , { } ] , 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 ) {
24502570var utils = require ( '../utils' ) [ 0 ] ( ) ;
24512571
24522572function lifecycleNoop ( resourceName , attrs , cb ) {
@@ -2656,7 +2776,7 @@ function DSProvider() {
26562776
26572777module . exports = DSProvider ;
26582778
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 ) {
26602780var errorPrefix = 'DS.bindAll(scope, expr, resourceName, params): ' ;
26612781
26622782/**
@@ -2728,7 +2848,7 @@ function bindOne(scope, expr, resourceName, params) {
27282848
27292849module . exports = bindOne ;
27302850
2731- } , { } ] , 42 :[ function ( require , module , exports ) {
2851+ } , { } ] , 43 :[ function ( require , module , exports ) {
27322852var errorPrefix = 'DS.bindOne(scope, expr, resourceName, id): ' ;
27332853
27342854/**
@@ -2788,7 +2908,7 @@ function bindOne(scope, expr, resourceName, id) {
27882908
27892909module . exports = bindOne ;
27902910
2791- } , { } ] , 43 :[ function ( require , module , exports ) {
2911+ } , { } ] , 44 :[ function ( require , module , exports ) {
27922912var errorPrefix = 'DS.changes(resourceName, id): ' ;
27932913
27942914/**
@@ -2845,7 +2965,7 @@ function changes(resourceName, id) {
28452965
28462966module . exports = changes ;
28472967
2848- } , { } ] , 44 :[ function ( require , module , exports ) {
2968+ } , { } ] , 45 :[ function ( require , module , exports ) {
28492969/*jshint evil:true*/
28502970var errorPrefix = 'DS.defineResource(definition): ' ;
28512971
@@ -2982,7 +3102,7 @@ function defineResource(definition) {
29823102
29833103module . exports = defineResource ;
29843104
2985- } , { } ] , 45 :[ function ( require , module , exports ) {
3105+ } , { } ] , 46 :[ function ( require , module , exports ) {
29863106var observe = require ( 'observejs' ) ;
29873107
29883108/**
@@ -3024,7 +3144,7 @@ function digest() {
30243144
30253145module . exports = digest ;
30263146
3027- } , { "observejs" :"QYwGEY" } ] , 46 :[ function ( require , module , exports ) {
3147+ } , { "observejs" :"QYwGEY" } ] , 47 :[ function ( require , module , exports ) {
30283148var errorPrefix = 'DS.eject(resourceName, id): ' ;
30293149
30303150function _eject ( definition , resource , id ) {
@@ -3107,7 +3227,7 @@ function eject(resourceName, id) {
31073227
31083228module . exports = eject ;
31093229
3110- } , { } ] , 47 :[ function ( require , module , exports ) {
3230+ } , { } ] , 48 :[ function ( require , module , exports ) {
31113231var errorPrefix = 'DS.ejectAll(resourceName[, params]): ' ;
31123232
31133233function _ejectAll ( definition , resource , params ) {
@@ -3211,7 +3331,7 @@ function ejectAll(resourceName, params) {
32113331
32123332module . exports = ejectAll ;
32133333
3214- } , { } ] , 48 :[ function ( require , module , exports ) {
3334+ } , { } ] , 49 :[ function ( require , module , exports ) {
32153335/* jshint loopfunc: true */
32163336var errorPrefix = 'DS.filter(resourceName, params[, options]): ' ;
32173337
@@ -3368,7 +3488,7 @@ function filter(resourceName, params, options) {
33683488
33693489module . exports = filter ;
33703490
3371- } , { } ] , 49 :[ function ( require , module , exports ) {
3491+ } , { } ] , 50 :[ function ( require , module , exports ) {
33723492var errorPrefix = 'DS.get(resourceName, id[, options]): ' ;
33733493
33743494/**
@@ -3431,7 +3551,7 @@ function get(resourceName, id, options) {
34313551
34323552module . exports = get ;
34333553
3434- } , { } ] , 50 :[ function ( require , module , exports ) {
3554+ } , { } ] , 51 :[ function ( require , module , exports ) {
34353555var errorPrefix = 'DS.hasChanges(resourceName, id): ' ;
34363556
34373557function diffIsEmpty ( utils , diff ) {
@@ -3494,7 +3614,7 @@ function hasChanges(resourceName, id) {
34943614
34953615module . exports = hasChanges ;
34963616
3497- } , { } ] , 51 :[ function ( require , module , exports ) {
3617+ } , { } ] , 52 :[ function ( require , module , exports ) {
34983618module . exports = {
34993619 /**
35003620 * @doc method
@@ -3637,7 +3757,7 @@ module.exports = {
36373757 hasChanges : require ( './hasChanges' )
36383758} ;
36393759
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 ) {
36413761var observe = require ( 'observejs' ) ,
36423762 errorPrefix = 'DS.inject(resourceName, attrs[, options]): ' ;
36433763
@@ -3787,7 +3907,7 @@ function inject(resourceName, attrs, options) {
37873907
37883908module . exports = inject ;
37893909
3790- } , { "observejs" :"QYwGEY" } ] , 53 :[ function ( require , module , exports ) {
3910+ } , { "observejs" :"QYwGEY" } ] , 54 :[ function ( require , module , exports ) {
37913911var errorPrefix = 'DS.lastModified(resourceName[, id]): ' ;
37923912
37933913/**
@@ -3845,7 +3965,7 @@ function lastModified(resourceName, id) {
38453965
38463966module . exports = lastModified ;
38473967
3848- } , { } ] , 54 :[ function ( require , module , exports ) {
3968+ } , { } ] , 55 :[ function ( require , module , exports ) {
38493969var errorPrefix = 'DS.lastSaved(resourceName[, id]): ' ;
38503970
38513971/**
@@ -3906,7 +4026,7 @@ function lastSaved(resourceName, id) {
39064026
39074027module . exports = lastSaved ;
39084028
3909- } , { } ] , 55 :[ function ( require , module , exports ) {
4029+ } , { } ] , 56 :[ function ( require , module , exports ) {
39104030var errorPrefix = 'DS.previous(resourceName, id): ' ;
39114031
39124032/**
@@ -4134,7 +4254,7 @@ module.exports = [function () {
41344254 } ;
41354255} ] ;
41364256
4137- } , { } ] , 58 :[ function ( require , module , exports ) {
4257+ } , { } ] , 59 :[ function ( require , module , exports ) {
41384258( function ( window , angular , undefined ) {
41394259 'use strict' ;
41404260
@@ -4143,7 +4263,7 @@ module.exports = [function () {
41434263 * @id angular-data
41444264 * @name angular-data
41454265 * @description
4146- * __Version:__ 0.9.0-SNAPSHOT
4266+ * __Version:__ 0.9.0
41474267 *
41484268 * ## Install
41494269 *
@@ -4216,7 +4336,7 @@ module.exports = [function () {
42164336
42174337} ) ( window , window . angular ) ;
42184338
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 ) {
42204340module . exports = [ function ( ) {
42214341 return {
42224342 isString : angular . isString ,
@@ -4300,4 +4420,4 @@ module.exports = [function () {
43004420
43014421} , { "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 ) {
43024422module . exports = require ( 'K0yknU' ) ;
4303- } , { } ] } , { } , [ 58 ] )
4423+ } , { } ] } , { } , [ 59 ] )
0 commit comments