1
- /**
2
- * @author Jason Dobry <[email protected] >
3
- * @file angular-data.js
4
- * @version 0.5.0 - Homepage <http://jmdobry.github.io/angular-data/>
5
- * @copyright (c) 2014 Jason Dobry <https://github.com/jmdobry/angular-data>
6
- * @license MIT <https://github.com/jmdobry/angular-data/blob/master/LICENSE>
7
- *
8
- * @overview Data store for Angular.js.
9
- */
10
1
require = ( function e ( t , n , r ) { function s ( o , u ) { if ( ! n [ o ] ) { if ( ! t [ o ] ) { var a = typeof require == "function" && require ; if ( ! u && a ) return a ( o , ! 0 ) ; if ( i ) return i ( o , ! 0 ) ; throw new Error ( "Cannot find module '" + o + "'" ) } var f = n [ o ] = { exports :{ } } ; t [ o ] [ 0 ] . call ( f . exports , function ( e ) { var n = t [ o ] [ 1 ] [ e ] ; return s ( n ?n :e ) } , f , f . exports , e , t , n , r ) } return n [ o ] . exports } var i = typeof require == "function" && require ; for ( var o = 0 ; o < r . length ; o ++ ) s ( r [ o ] ) ; return s } ) ( { 1 :[ function ( require , module , exports ) {
11
2
var indexOf = require ( './indexOf' ) ;
12
3
@@ -1924,31 +1915,36 @@ var utils = require('utils'),
1924
1915
* - `{UnhandledError}`
1925
1916
*/
1926
1917
function destroy ( resourceName , id ) {
1927
- var deferred = service . $q . defer ( ) ;
1918
+ var deferred = services . $q . defer ( ) ,
1919
+ promise = deferred . promise ;
1920
+
1928
1921
if ( ! services . store [ resourceName ] ) {
1929
1922
deferred . reject ( new errors . RuntimeError ( errorPrefix + resourceName + ' is not a registered resource!' ) ) ;
1930
1923
} else if ( ! utils . isString ( id ) && ! utils . isNumber ( id ) ) {
1931
1924
deferred . reject ( new errors . IllegalArgumentError ( errorPrefix + 'id: Must be a string or a number!' , { id : { actual : typeof id , expected : 'string|number' } } ) ) ;
1932
- }
1933
-
1934
- try {
1925
+ } else {
1935
1926
var resource = services . store [ resourceName ] ,
1936
- _this = this ,
1937
- url = utils . makePath ( resource . baseUrl || services . config . baseUrl , resource . endpoint || resource . name , id ) ;
1927
+ _this = this ;
1938
1928
1939
- _this . DEL ( url , null ) . then ( function ( ) {
1940
- try {
1929
+ promise = promise
1930
+ . then ( function ( attrs ) {
1931
+ return services . $q . promisify ( resource . beforeDestroy ) ( resourceName , attrs ) ;
1932
+ } )
1933
+ . then ( function ( ) {
1934
+ return _this . DEL ( utils . makePath ( resource . baseUrl , resource . endpoint , id ) , null ) ;
1935
+ } )
1936
+ . then ( function ( ) {
1937
+ return services . $q . promisify ( resource . afterDestroy ) ( resourceName , resource . index [ id ] ) ;
1938
+ } )
1939
+ . then ( function ( ) {
1941
1940
_this . eject ( resourceName , id ) ;
1942
- deferred . resolve ( id ) ;
1943
- } catch ( err ) {
1944
- deferred . reject ( err ) ;
1945
- }
1946
- } , deferred . reject ) ;
1947
- } catch ( err ) {
1948
- deferred . reject ( new errors . UnhandledError ( err ) ) ;
1941
+ return id ;
1942
+ } ) ;
1943
+
1944
+ deferred . resolve ( resource . index [ id ] ) ;
1949
1945
}
1950
1946
1951
- return deferred . promise ;
1947
+ return promise ;
1952
1948
}
1953
1949
1954
1950
module . exports = destroy ;
@@ -2005,7 +2001,9 @@ var utils = require('utils'),
2005
2001
* - `{UnhandledError}`
2006
2002
*/
2007
2003
function find ( resourceName , id , options ) {
2008
- var deferred = services . $q . defer ( ) ;
2004
+ var deferred = services . $q . defer ( ) ,
2005
+ promise = deferred . promise ;
2006
+
2009
2007
options = options || { } ;
2010
2008
2011
2009
if ( ! services . store [ resourceName ] ) {
@@ -2015,43 +2013,35 @@ function find(resourceName, id, options) {
2015
2013
} else if ( ! utils . isObject ( options ) ) {
2016
2014
deferred . reject ( new errors . IllegalArgumentError ( errorPrefix + 'options: Must be an object!' , { options : { actual : typeof options , expected : 'object' } } ) ) ;
2017
2015
} else {
2018
- var _this = this ;
2019
-
2020
2016
try {
2021
- var resource = services . store [ resourceName ] ;
2017
+ var resource = services . store [ resourceName ] ,
2018
+ _this = this ;
2022
2019
2023
- if ( id in resource . index && ! options . bypassCache ) {
2024
- deferred . resolve ( _this . get ( resourceName , id ) ) ;
2025
- } else {
2026
- var url = utils . makePath ( resource . baseUrl || services . config . baseUrl , resource . endpoint || resource . name , id ) ,
2027
- config = null ;
2020
+ if ( options . bypassCache ) {
2021
+ delete resource . completedQueries [ id ] ;
2022
+ }
2028
2023
2029
- if ( options . bypassCache ) {
2030
- config = {
2031
- headers : {
2032
- 'Last-Modified' : new Date ( resource . modified [ id ] )
2033
- }
2034
- } ;
2024
+ if ( ! ( id in resource . completedQueries ) ) {
2025
+ if ( ! ( id in resource . pendingQueries ) ) {
2026
+ promise = resource . pendingQueries [ id ] = GET ( utils . makePath ( resource . baseUrl , resource . endpoint , id ) , null )
2027
+ . then ( function ( data ) {
2028
+ // Query is no longer pending
2029
+ delete resource . pendingQueries [ id ] ;
2030
+ resource . completedQueries [ id ] = new Date ( ) . getTime ( ) ;
2031
+ return _this . inject ( resourceName , data ) ;
2032
+ } ) ;
2035
2033
}
2036
- GET ( url , config ) . then ( function ( data ) {
2037
- try {
2038
- _this . inject ( resourceName , data ) ;
2039
- deferred . resolve ( _this . get ( resourceName , id ) ) ;
2040
- } catch ( err ) {
2041
- deferred . reject ( err ) ;
2042
- }
2043
- } , deferred . reject ) ;
2044
- }
2045
- } catch ( err ) {
2046
- if ( ! ( err instanceof errors . UnhandledError ) ) {
2047
- deferred . reject ( new errors . UnhandledError ( err ) ) ;
2034
+
2035
+ return resource . pendingQueries [ id ] ;
2048
2036
} else {
2049
- deferred . reject ( err ) ;
2037
+ deferred . resolve ( _this . get ( resourceName , id ) ) ;
2050
2038
}
2039
+ } catch ( err ) {
2040
+ deferred . reject ( err ) ;
2051
2041
}
2052
2042
}
2053
2043
2054
- return deferred . promise ;
2044
+ return promise ;
2055
2045
}
2056
2046
2057
2047
module . exports = find ;
@@ -2087,9 +2077,8 @@ function processResults(data, resourceName, queryHash) {
2087
2077
2088
2078
function _findAll ( resourceName , params , options ) {
2089
2079
var resource = services . store [ resourceName ] ,
2090
- _this = this ;
2091
-
2092
- var queryHash = utils . toJson ( params ) ;
2080
+ _this = this ,
2081
+ queryHash = utils . toJson ( params ) ;
2093
2082
2094
2083
if ( options . bypassCache ) {
2095
2084
delete resource . completedQueries [ queryHash ] ;
@@ -2098,7 +2087,7 @@ function _findAll(resourceName, params, options) {
2098
2087
if ( ! ( queryHash in resource . completedQueries ) ) {
2099
2088
// This particular query has never been completed
2100
2089
2101
- if ( ! resource . pendingQueries [ queryHash ] ) {
2090
+ if ( ! ( queryHash in resource . pendingQueries ) ) {
2102
2091
2103
2092
// This particular query has never even been made
2104
2093
resource . pendingQueries [ queryHash ] = GET ( utils . makePath ( resource . baseUrl , resource . endpoint ) , { params : params } )
@@ -2110,6 +2099,7 @@ function _findAll(resourceName, params, options) {
2110
2099
}
2111
2100
} ) ;
2112
2101
}
2102
+
2113
2103
return resource . pendingQueries [ queryHash ] ;
2114
2104
} else {
2115
2105
return this . filter ( resourceName , params , options ) ;
@@ -2413,7 +2403,7 @@ function save(resourceName, id, options) {
2413
2403
} else if ( ! utils . isString ( id ) && ! utils . isNumber ( id ) ) {
2414
2404
deferred . reject ( new errors . IllegalArgumentError ( errorPrefix + 'id: Must be a string or a number!' , { id : { actual : typeof id , expected : 'string|number' } } ) ) ;
2415
2405
} else if ( ! utils . isObject ( options ) ) {
2416
- deferred . reject ( new errors . IllegalArgumentError ( errorPrefix + 'id : Must be an object!' , { options : { actual : typeof options , expected : 'object' } } ) ) ;
2406
+ deferred . reject ( new errors . IllegalArgumentError ( errorPrefix + 'options : Must be an object!' , { options : { actual : typeof options , expected : 'object' } } ) ) ;
2417
2407
} else if ( ! ( id in services . store [ resourceName ] . index ) ) {
2418
2408
deferred . reject ( new errors . RuntimeError ( errorPrefix + 'id: "' + id + '" not found!' ) ) ;
2419
2409
} else {
@@ -2707,15 +2697,30 @@ var utils = require('utils'),
2707
2697
* ```js
2708
2698
* DSProvider.config({
2709
2699
* baseUrl: 'http://myapp.com/api',
2710
- * idAttribute: '_id'
2700
+ * idAttribute: '_id',
2701
+ * validate: function (resourceName, attrs, cb) {
2702
+ * console.log('looks good to me');
2703
+ * cb(null, attrs);
2704
+ * }
2711
2705
* });
2712
2706
* ```
2713
2707
*
2714
2708
* ## Throws:
2715
2709
*
2716
2710
* - `{IllegalArgumentError}`
2717
2711
*
2718
- * @param {object } options Configuration for the data store.
2712
+ * @param {object } options Global configuration for the data store. Properties:
2713
+ * - `{string=}` - `baseUrl` - The default base url to be used by the data store. Can be overridden via `DS.defineResource`.
2714
+ * - `{string=}` - `idAttribute` - The default property that specifies the primary key of an object. Default: `"id"`.
2715
+ * - `{function=}` - `beforeValidate` - Global lifecycle hook. Signature: `beforeValidate(resourceName, attrs, cb)`. Callback signature: `cb(err, attrs)`.
2716
+ * - `{function=}` - `validate` - Global lifecycle hook. Signature: `validate(resourceName, attrs, cb)`. Callback signature: `cb(err, attrs)`.
2717
+ * - `{function=}` - `afterValidate` - Global lifecycle hook. Signature: `afterValidate(resourceName, attrs, cb)`. Callback signature: `cb(err, attrs)`.
2718
+ * - `{function=}` - `beforeCreate` - Global lifecycle hook. Signature: `beforeCreate(resourceName, attrs, cb)`. Callback signature: `cb(err, attrs)`.
2719
+ * - `{function=}` - `afterCreate` - Global lifecycle hook. Signature: `afterCreate(resourceName, attrs, cb)`. Callback signature: `cb(err, attrs)`.
2720
+ * - `{function=}` - `beforeUpdate` - Global lifecycle hook. Signature: `beforeUpdate(resourceName, attrs, cb)`. Callback signature: `cb(err, attrs)`.
2721
+ * - `{function=}` - `afterUpdate` - Global lifecycle hook. Signature: `afterUpdate(resourceName, attrs, cb)`. Callback signature: `cb(err, attrs)`.
2722
+ * - `{function=}` - `beforeDestroy` - Global lifecycle hook. Signature: `beforeDestroy(resourceName, attrs, cb)`. Callback signature: `cb(err, attrs)`.
2723
+ * - `{function=}` - `afterDestroy` - Global lifecycle hook. Signature: `afterDestroy(resourceName, attrs, cb)`. Callback signature: `cb(err, attrs)`.
2719
2724
*/
2720
2725
function config ( options ) {
2721
2726
options = options || { } ;
@@ -2968,6 +2973,8 @@ function Resource(options) {
2968
2973
this . collectionModified = 0 ;
2969
2974
}
2970
2975
2976
+ Resource . prototype = services . config ;
2977
+
2971
2978
/**
2972
2979
* @doc method
2973
2980
* @id DS.sync_methods:defineResource
@@ -3006,8 +3013,16 @@ function Resource(options) {
3006
3013
* - `{string}` - `name` - The name by which this resource will be identified.
3007
3014
* - `{string="id"}` - `idAttribute` - The attribute that specifies the primary key for this resource.
3008
3015
* - `{string=}` - `endpoint` - The attribute that specifies the primary key for this resource. Default is the value of `name`.
3009
- * - `{string="/"}` - `baseUrl` - The url relative to which all AJAX requests will be made.
3010
- * - `{function=}` - `validate` - The validation function to be executed before create operations.
3016
+ * - `{string=}` - `baseUrl` - The url relative to which all AJAX requests will be made.
3017
+ * - `{function=}` - `beforeValidate` - Lifecycle hook. Overrides global. Signature: `beforeValidate(resourceName, attrs, cb)`. Callback signature: `cb(err, attrs)`.
3018
+ * - `{function=}` - `validate` - Lifecycle hook. Overrides global. Signature: `validate(resourceName, attrs, cb)`. Callback signature: `cb(err, attrs)`.
3019
+ * - `{function=}` - `afterValidate` - Lifecycle hook. Overrides global. Signature: `afterValidate(resourceName, attrs, cb)`. Callback signature: `cb(err, attrs)`.
3020
+ * - `{function=}` - `beforeCreate` - Lifecycle hook. Overrides global. Signature: `beforeCreate(resourceName, attrs, cb)`. Callback signature: `cb(err, attrs)`.
3021
+ * - `{function=}` - `afterCreate` - Lifecycle hook. Overrides global. Signature: `afterCreate(resourceName, attrs, cb)`. Callback signature: `cb(err, attrs)`.
3022
+ * - `{function=}` - `beforeUpdate` - Lifecycle hook. Overrides global. Signature: `beforeUpdate(resourceName, attrs, cb)`. Callback signature: `cb(err, attrs)`.
3023
+ * - `{function=}` - `afterUpdate` - Lifecycle hook. Overrides global. Signature: `afterUpdate(resourceName, attrs, cb)`. Callback signature: `cb(err, attrs)`.
3024
+ * - `{function=}` - `beforeDestroy` - Lifecycle hook. Overrides global. Signature: `beforeDestroy(resourceName, attrs, cb)`. Callback signature: `cb(err, attrs)`.
3025
+ * - `{function=}` - `afterDestroy` - Lifecycle hook. Overrides global. Signature: `afterDestroy(resourceName, attrs, cb)`. Callback signature: `cb(err, attrs)`.
3011
3026
*/
3012
3027
function defineResource ( definition ) {
3013
3028
if ( utils . isString ( definition ) ) {
@@ -3026,7 +3041,6 @@ function defineResource(definition) {
3026
3041
}
3027
3042
3028
3043
try {
3029
- Resource . prototype = services . config ;
3030
3044
services . store [ definition . name ] = new Resource ( definition ) ;
3031
3045
} catch ( err ) {
3032
3046
delete services . store [ definition . name ] ;
@@ -3405,7 +3419,9 @@ function get(resourceName, id, options) {
3405
3419
try {
3406
3420
// cache miss, request resource from server
3407
3421
if ( ! ( id in services . store [ resourceName ] . index ) && options . loadFromServer ) {
3408
- this . find ( resourceName , id ) ;
3422
+ this . find ( resourceName , id ) . then ( null , function ( err ) {
3423
+ throw err ;
3424
+ } ) ;
3409
3425
}
3410
3426
3411
3427
// return resource from cache
@@ -3910,9 +3926,7 @@ function previous(resourceName, id) {
3910
3926
3911
3927
module . exports = previous ;
3912
3928
3913
- } , { "errors" :"hIh4e1" , "services" :"cX8q+p" , "utils" :"uE/lJt" } ] , "errors" :[ function ( require , module , exports ) {
3914
- module . exports = require ( 'hIh4e1' ) ;
3915
- } , { } ] , "hIh4e1" :[ function ( require , module , exports ) {
3929
+ } , { "errors" :"hIh4e1" , "services" :"cX8q+p" , "utils" :"uE/lJt" } ] , "hIh4e1" :[ function ( require , module , exports ) {
3916
3930
/**
3917
3931
* @doc function
3918
3932
* @id errors.types:UnhandledError
@@ -4124,6 +4138,8 @@ module.exports = {
4124
4138
RuntimeError : RuntimeError
4125
4139
} ;
4126
4140
4141
+ } , { } ] , "errors" :[ function ( require , module , exports ) {
4142
+ module . exports = require ( 'hIh4e1' ) ;
4127
4143
} , { } ] , 52 :[ function ( require , module , exports ) {
4128
4144
( function ( window , angular , undefined ) {
4129
4145
'use strict' ;
@@ -4206,6 +4222,7 @@ module.exports = {
4206
4222
isArray : angular . isArray ,
4207
4223
isObject : angular . isObject ,
4208
4224
isNumber : angular . isNumber ,
4225
+ isFunction : angular . isFunction ,
4209
4226
isEmpty : require ( 'mout/lang/isEmpty' ) ,
4210
4227
toJson : angular . toJson ,
4211
4228
makePath : require ( 'mout/string/makePath' ) ,
@@ -4279,4 +4296,4 @@ module.exports = {
4279
4296
4280
4297
} , { "mout/array/contains" :1 , "mout/array/filter" :2 , "mout/array/slice" :5 , "mout/array/sort" :6 , "mout/array/toLookup" :7 , "mout/lang/isEmpty" :12 , "mout/object/deepMixIn" :19 , "mout/object/forOwn" :21 , "mout/string/makePath" :23 , "mout/string/upperCase" :24 } ] , "utils" :[ function ( require , module , exports ) {
4281
4298
module . exports = require ( 'uE/lJt' ) ;
4282
- } , { } ] } , { } , [ 52 ] )
4299
+ } , { } ] } , { } , [ 52 ] )
0 commit comments