blob: bef085c03bfa16be8ab7686e891d7d6e25c18bc5 (
plain)
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
|
var isarray = require('lodash.isarray');
var pluralize = require('pluralize');
var Q = require('q');
/**
* OKUtils!
* Cross cutting concerns n stuff
*/
module.exports = {
/**
* Takes a meta data query and an array of resource queries
* and returns a promise for an object merging all queried
* data, pluralizing keys where necessary.
*
* Lil bit convoluted, sorry.
*/
fetchTemplateData: function fetchTemplateData(meta, queries, options) {
return Q.promise(function(resolve, reject) {
return Q.all(
[meta.get()].concat(queries.map(function(query) {
return query.get(options);
})))
.then(function(results) {
var metadata = results.shift();
var normalized = results.reduce(function(data, result, i) {
var type = queries[i].type;
if (isarray(result)) {
data[pluralize(type)] = result;
} else {
data[type] = result;
}
return data;
}, {meta: metadata});
resolve(normalized);
}, reject);
});
}
};
|