@@ -36,6 +36,11 @@ function errorPrefix(resourceName, id) {
36
36
* @param {object= } options Optional configuration. Also passed along to the adapter's `update` method. Properties:
37
37
*
38
38
* - `{boolean=}` - `cacheResponse` - Inject the data returned by the adapter into the data store. Default: `true`.
39
+ * - `{function=}` - `beforeValidate` - Override the resource or global lifecycle hook.
40
+ * - `{function=}` - `validate` - Override the resource or global lifecycle hook.
41
+ * - `{function=}` - `afterValidate` - Override the resource or global lifecycle hook.
42
+ * - `{function=}` - `beforeUpdate` - Override the resource or global lifecycle hook.
43
+ * - `{function=}` - `afterUpdate` - Override the resource or global lifecycle hook.
39
44
*
40
45
* @returns {Promise } Promise produced by the `$q` service.
41
46
*
@@ -51,14 +56,14 @@ function errorPrefix(resourceName, id) {
51
56
function update ( resourceName , id , attrs , options ) {
52
57
var DS = this ;
53
58
var deferred = DS . $q . defer ( ) ;
54
- var promise = deferred . promise ;
55
59
56
60
try {
57
61
var IA = DS . errors . IA ;
62
+ var definition = DS . definitions [ resourceName ] ;
58
63
59
64
options = options || { } ;
60
65
61
- if ( ! DS . definitions [ resourceName ] ) {
66
+ if ( ! definition ) {
62
67
throw new DS . errors . NER ( errorPrefix ( resourceName , id ) + resourceName ) ;
63
68
} else if ( ! DS . utils . isString ( id ) && ! DS . utils . isNumber ( id ) ) {
64
69
throw new IA ( errorPrefix ( resourceName , id ) + 'id: Must be a string or a number!' ) ;
@@ -68,50 +73,54 @@ function update(resourceName, id, attrs, options) {
68
73
throw new IA ( errorPrefix ( resourceName , id ) + 'options: Must be an object!' ) ;
69
74
}
70
75
71
- var definition = DS . definitions [ resourceName ] ;
72
- var resource = DS . store [ resourceName ] ;
73
-
74
76
if ( ! ( 'cacheResponse' in options ) ) {
75
77
options . cacheResponse = true ;
76
78
}
77
79
78
- promise = promise
80
+ deferred . resolve ( attrs ) ;
81
+
82
+ return deferred . promise
79
83
. then ( function ( attrs ) {
80
- return DS . $q . promisify ( definition . beforeValidate ) ( resourceName , attrs ) ;
84
+ var func = options . beforeValidate ? DS . $q . promisify ( options . beforeValidate ) : definition . beforeValidate ;
85
+ return func . call ( attrs , resourceName , attrs ) ;
81
86
} )
82
87
. then ( function ( attrs ) {
83
- return DS . $q . promisify ( definition . validate ) ( resourceName , attrs ) ;
88
+ var func = options . validate ? DS . $q . promisify ( options . validate ) : definition . validate ;
89
+ return func . call ( attrs , resourceName , attrs ) ;
84
90
} )
85
91
. then ( function ( attrs ) {
86
- return DS . $q . promisify ( definition . afterValidate ) ( resourceName , attrs ) ;
92
+ var func = options . afterValidate ? DS . $q . promisify ( options . afterValidate ) : definition . afterValidate ;
93
+ return func . call ( attrs , resourceName , attrs ) ;
87
94
} )
88
95
. then ( function ( attrs ) {
89
- return DS . $q . promisify ( definition . beforeUpdate ) ( resourceName , attrs ) ;
96
+ var func = options . beforeUpdate ? DS . $q . promisify ( options . beforeUpdate ) : definition . beforeUpdate ;
97
+ return func . call ( attrs , resourceName , attrs ) ;
90
98
} )
91
99
. then ( function ( attrs ) {
92
100
return DS . adapters [ options . adapter || definition . defaultAdapter ] . update ( definition , id , definition . serialize ( resourceName , attrs ) , options ) ;
93
101
} )
94
102
. then ( function ( res ) {
95
- return DS . $q . promisify ( definition . afterUpdate ) ( resourceName , definition . deserialize ( resourceName , res ) ) ;
103
+ var func = options . afterUpdate ? DS . $q . promisify ( options . afterUpdate ) : definition . afterUpdate ;
104
+ var attrs = definition . deserialize ( resourceName , res ) ;
105
+ return func . call ( attrs , resourceName , attrs ) ;
96
106
} )
97
107
. then ( function ( data ) {
98
108
if ( options . cacheResponse ) {
109
+ var resource = DS . store [ resourceName ] ;
99
110
var updated = DS . inject ( definition . name , data , options ) ;
100
111
var id = updated [ definition . idAttribute ] ;
101
112
resource . previousAttributes [ id ] = DS . utils . deepMixIn ( { } , updated ) ;
102
113
resource . saved [ id ] = DS . utils . updateTimestamp ( resource . saved [ id ] ) ;
114
+ resource . observers [ id ] . discardChanges ( ) ;
103
115
return DS . get ( definition . name , id ) ;
104
116
} else {
105
117
return data ;
106
118
}
107
119
} ) ;
108
-
109
- deferred . resolve ( attrs ) ;
110
120
} catch ( err ) {
111
121
deferred . reject ( err ) ;
122
+ return deferred . promise ;
112
123
}
113
-
114
- return promise ;
115
124
}
116
125
117
126
module . exports = update ;
0 commit comments