1
- var errorPrefix = 'DS.findAll(resourceName, params[, options]): ' ;
1
+ function errorPrefix ( resourceName ) {
2
+ return 'DS.findAll(' + resourceName + ', params[, options]): ' ;
3
+ }
2
4
3
- function processResults ( utils , data , resourceName , queryHash ) {
4
- var resource = this . store [ resourceName ] ,
5
- idAttribute = this . definitions [ resourceName ] . idAttribute ,
6
- date = new Date ( ) . getTime ( ) ;
5
+ function processResults ( data , resourceName , queryHash ) {
6
+ var resource = this . store [ resourceName ] ;
7
+ var idAttribute = this . definitions [ resourceName ] . idAttribute ;
8
+ var date = new Date ( ) . getTime ( ) ;
7
9
8
10
data = data || [ ] ;
9
11
@@ -12,24 +14,31 @@ function processResults(utils, data, resourceName, queryHash) {
12
14
resource . completedQueries [ queryHash ] = date ;
13
15
14
16
// Update modified timestamp of collection
15
- resource . collectionModified = utils . updateTimestamp ( resource . collectionModified ) ;
17
+ resource . collectionModified = this . utils . updateTimestamp ( resource . collectionModified ) ;
16
18
17
19
// Merge the new values into the cache
18
20
var injected = this . inject ( resourceName , data ) ;
19
21
20
22
// Make sure each object is added to completedQueries
21
- angular . forEach ( injected , function ( item ) {
22
- resource . completedQueries [ item [ idAttribute ] ] = date ;
23
- } ) ;
23
+ if ( this . utils . isArray ( injected ) ) {
24
+ angular . forEach ( injected , function ( item ) {
25
+ if ( item && item [ idAttribute ] ) {
26
+ resource . completedQueries [ item [ idAttribute ] ] = date ;
27
+ }
28
+ } ) ;
29
+ } else {
30
+ this . $log . warn ( errorPrefix ( resourceName ) + 'response is expected to be an array!' ) ;
31
+ resource . completedQueries [ injected [ idAttribute ] ] = date ;
32
+ }
24
33
25
34
return injected ;
26
35
}
27
36
28
- function _findAll ( utils , resourceName , params , options ) {
29
- var definition = this . definitions [ resourceName ] ,
30
- resource = this . store [ resourceName ] ,
31
- _this = this ,
32
- queryHash = utils . toJson ( params ) ;
37
+ function _findAll ( resourceName , params , options ) {
38
+ var definition = this . definitions [ resourceName ] ;
39
+ var resource = this . store [ resourceName ] ;
40
+ var _this = this ;
41
+ var queryHash = _this . utils . toJson ( params ) ;
33
42
34
43
if ( options . bypassCache || ! options . cacheResponse ) {
35
44
delete resource . completedQueries [ queryHash ] ;
@@ -46,7 +55,7 @@ function _findAll(utils, resourceName, params, options) {
46
55
var data = definition . deserialize ( resourceName , res ) ;
47
56
if ( options . cacheResponse ) {
48
57
try {
49
- return processResults . apply ( _this , [ utils , data , resourceName , queryHash ] ) ;
58
+ return processResults . apply ( _this , [ data , resourceName , queryHash ] ) ;
50
59
} catch ( err ) {
51
60
return _this . $q . reject ( err ) ;
52
61
}
@@ -139,19 +148,19 @@ function findAll(resourceName, params, options) {
139
148
params = params || { } ;
140
149
141
150
if ( ! this . definitions [ resourceName ] ) {
142
- throw new this . errors . NER ( errorPrefix + resourceName ) ;
151
+ throw new this . errors . NER ( errorPrefix ( resourceName ) + resourceName ) ;
143
152
} else if ( ! this . utils . isObject ( params ) ) {
144
- throw new IA ( errorPrefix + 'params: Must be an object!' ) ;
153
+ throw new IA ( errorPrefix ( resourceName ) + 'params: Must be an object!' ) ;
145
154
} else if ( ! this . utils . isObject ( options ) ) {
146
- throw new IA ( errorPrefix + 'options: Must be an object!' ) ;
155
+ throw new IA ( errorPrefix ( resourceName ) + 'options: Must be an object!' ) ;
147
156
}
148
157
149
158
if ( ! ( 'cacheResponse' in options ) ) {
150
159
options . cacheResponse = true ;
151
160
}
152
161
153
162
promise = promise . then ( function ( ) {
154
- return _findAll . apply ( _this , [ _this . utils , resourceName , params , options ] ) ;
163
+ return _findAll . apply ( _this , [ resourceName , params , options ] ) ;
155
164
} ) ;
156
165
deferred . resolve ( ) ;
157
166
} catch ( err ) {
0 commit comments