-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathfindAll.js
158 lines (142 loc) · 4.89 KB
/
findAll.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
var errorPrefix = 'DS.findAll(resourceName, params[, options]): ';
function processResults(utils, data, resourceName, queryHash) {
var resource = this.store[resourceName];
data = data || [];
// Query is no longer pending
delete resource.pendingQueries[queryHash];
resource.completedQueries[queryHash] = new Date().getTime();
// Merge the new values into the cache
this.inject(resourceName, data);
// Update modified timestamp of collection
resource.collectionModified = utils.updateTimestamp(resource.collectionModified);
return data;
}
function _findAll(utils, resourceName, params, options) {
var definition = this.definitions[resourceName],
resource = this.store[resourceName],
_this = this,
queryHash = utils.toJson(params);
if (options.bypassCache) {
delete resource.completedQueries[queryHash];
}
if (!(queryHash in resource.completedQueries)) {
// This particular query has never been completed
if (!(queryHash in resource.pendingQueries)) {
// This particular query has never even been made
resource.pendingQueries[queryHash] = _this.adapters[options.adapter || definition.defaultAdapter].findAll(definition, params, options)
.then(function (res) {
var data = definition.deserialize(resourceName, res);
if (options.cacheResponse) {
try {
return processResults.apply(_this, [utils, data, resourceName, queryHash]);
} catch (err) {
throw new _this.errors.UnhandledError(err);
}
} else {
return data;
}
}, function (err) {
delete resource.pendingQueries[queryHash];
return err;
});
}
return resource.pendingQueries[queryHash];
} else {
return this.filter(resourceName, params, options);
}
}
/**
* @doc method
* @id DS.async_methods:findAll
* @name findAll
* @description
* Asynchronously return the resource from the server filtered by the query. The results will be added to the data
* store when it returns from the server.
*
* ## Signature:
* ```js
* DS.findAll(resourceName, params[, options])
* ```
*
* ## Example:
*
* ```js
* var query = {
* where: {
* author: {
* '==': 'John Anderson'
* }
* }
* };
*
* DS.findAll('document', {
* query: query
* }).then(function (documents) {
* documents; // [{ id: 'aab7ff66-e21e-46e2-8be8-264d82aee535', author: 'John Anderson', title: 'How to cook' },
* // { id: 'ee7f3f4d-98d5-4934-9e5a-6a559b08479f', author: 'John Anderson', title: 'How NOT to cook' }]
*
* // The documents are now in the data store
* DS.filter('document', {
* query: query
* }); // [{ id: 'aab7ff66-e21e-46e2-8be8-264d82aee535', author: 'John Anderson', title: 'How to cook' },
* // { id: 'ee7f3f4d-98d5-4934-9e5a-6a559b08479f', author: 'John Anderson', title: 'How NOT to cook' }]
*
* }, function (err) {
* // handle error
* });
* ```
*
* @param {string} resourceName The resource type, e.g. 'user', 'comment', etc.
* @param {object} params Parameter object that is serialized into the query string. Properties:
*
* - `{object=}` - `query` - The query object by which to filter items of the type specified by `resourceName`. Properties:
* - `{object=}` - `where` - Where clause.
* - `{number=}` - `limit` - Limit clause.
* - `{skip=}` - `skip` - Skip clause.
* - `{orderBy=}` - `orderBy` - OrderBy clause.
*
* @param {object=} options Optional configuration. Properties:
* - `{boolean=}` - `bypassCache` - Bypass the cache. Default: `false`.
* - `{boolean=}` - `cacheResponse` - Inject the data returned by the server into the data store. Default: `true`.
*
* @returns {Promise} Promise produced by the `$q` service.
*
* ## Resolves with:
*
* - `{array}` - `items` - The collection of items returned by the server.
*
* ## Rejects with:
*
* - `{IllegalArgumentError}`
* - `{RuntimeError}`
* - `{UnhandledError}`
*/
function findAll(resourceName, params, options) {
var deferred = this.$q.defer(),
promise = deferred.promise,
_this = this;
options = options || {};
if (!this.definitions[resourceName]) {
deferred.reject(new this.errors.RuntimeError(errorPrefix + resourceName + ' is not a registered resource!'));
} else if (!this.utils.isObject(params)) {
deferred.reject(new this.errors.IllegalArgumentError(errorPrefix + 'params: Must be an object!'));
} else if (!this.utils.isObject(options)) {
deferred.reject(new this.errors.IllegalArgumentError(errorPrefix + 'options: Must be an object!'));
} else {
if (!('cacheResponse' in options)) {
options.cacheResponse = true;
} else {
options.cacheResponse = !!options.cacheResponse;
}
try {
promise = promise.then(function () {
return _findAll.apply(_this, [_this.utils, resourceName, params, options]);
});
deferred.resolve();
} catch (err) {
deferred.reject(new this.errors.UnhandledError(err));
}
}
return promise;
}
module.exports = findAll;