diff options
| -rw-r--r-- | app/node_modules/okutil/index.js | 39 | ||||
| -rw-r--r-- | app/node_modules/okview/index.js | 56 | ||||
| -rw-r--r-- | app/node_modules/okview/package.json | 7 |
3 files changed, 61 insertions, 41 deletions
diff --git a/app/node_modules/okutil/index.js b/app/node_modules/okutil/index.js index 01041b6..3142ae1 100644 --- a/app/node_modules/okutil/index.js +++ b/app/node_modules/okutil/index.js @@ -10,45 +10,6 @@ var Q = require('q'); 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(cache, result, i) { - var resource = queries[i].resource; - var type = queries[i].type; - var plural = pluralize(type); - if (isarray(result)) { - result = result.map(function(data) { - // Inform template of ID in generic way - data.id = data[resource.idField]; - return data; - }); - } else { - // Inform template of ID in generic way - result.id = result[resource.idField]; - result = [result] - } - cache[plural] = cache[plural] || []; - cache[plural] = cache[plural].concat(result); - return cache; - }, {meta: metadata}); - resolve(normalized); - }).fail(reject); - }); - }, - - /** * Return a copy of the route with a trailing slash */ withTrailingSlash: function withTrailingSlash(route) { diff --git a/app/node_modules/okview/index.js b/app/node_modules/okview/index.js index 1ceac03..5f3a8c9 100644 --- a/app/node_modules/okview/index.js +++ b/app/node_modules/okview/index.js @@ -1,4 +1,7 @@ -var fetchTemplateData = require('okutil').fetchTemplateData; +var assign = require('object-assign'); +var pluralize = require('pluralize'); +var isarray = require('lodash.isarray'); +var Q = require('q'); var OKResource = require('okresource'); // Routes for views over collections have a special pattern @@ -129,4 +132,55 @@ function getParamName(route) { return matches[1]; } +/** + * 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. + */ +function fetchTemplateData(meta, queries) { + return Q.promise(function(resolve, reject) { + return Q.all( + [meta.get()].concat(queries.map(function(query) { + return query.get(); + }))) + .then(function(results) { + var metadata = results.shift(); + var normalized = results.reduce(function(cache, result, i) { + // Huh? Bail + if (!result) + return cache; + var resource = queries[i].resource; + var type = queries[i].type; + var manyResult = isarray(result); + // Inform template of ID in generic field + if (manyResult) { + result = result.map(function(data) { + return assign({}, data, {id: data[resource.idField]}) + }); + } else { + result = assign({}, result, {id: result[resource.idField]}); + } + // If we have a lot of results for a certain type, + // we pluralize the key and yield an array of results + if (cache[type] || manyResult) { + var plural = pluralize(type); + delete cache[type]; + cache[plural] = []; + if (manyResult) { + cache[plural] = cache[plural].concat(result); + } else { + cache[plural].push(result); + } + } else { + cache[type] = result; + } + return cache; + }, {meta: metadata}); + resolve(normalized); + }).fail(reject); + }); +} + module.exports = OKView; diff --git a/app/node_modules/okview/package.json b/app/node_modules/okview/package.json index 8d95b40..bbf4e40 100644 --- a/app/node_modules/okview/package.json +++ b/app/node_modules/okview/package.json @@ -8,5 +8,10 @@ }, "author": "OKFocus", "license": "None", - "dependencies": {} + "dependencies": { + "lodash.isarray": "^3.0.1", + "object-assign": "^2.0.0", + "pluralize": "^1.1.2", + "q": "^1.2.0" + } } |
