From 63214f5e9e69c640701e6d92dc4a9bee9ec91cba Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Fri, 13 Jul 2012 23:32:20 +0200 Subject: o Adding toObject on each item. --- index.js | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) (limited to 'index.js') diff --git a/index.js b/index.js index f467018..8e2709a 100644 --- a/index.js +++ b/index.js @@ -12,6 +12,12 @@ function fromObject(root) { c.items = _.isArray(c.items) ? c.items : []; _.each(c.items, function(item) { item.links = _.isArray(item.links) ? item.links : []; + item.toObject = function() { + return _.reduce(item.data, function(map, field) { + map[field.name] = field.value; + return map; + }, {}); + } }); c.links = _.isArray(c.links) ? c.links : []; @@ -21,19 +27,13 @@ function fromObject(root) { query.data = _.isArray(query.data) ? query.data : []; }); - c.templates = _.isArray(c.templates) ? c.templates : []; + c.template = _.isObject(c.template) ? c.template : undefined; + // c.templates = _.isArray(c.templates) ? c.templates : []; // TODO: make un-enumerable - root.mapItemData = function(f) { + root.mapItems = function(f) { return _.map(this.collection.items, function(item) { -// console.log("item", item); - var map = _.reduce(item.data, function(map, field) { -// console.log("field", field); - map[field.name] = field.value; - return map; - }, {}); -// console.log("map", map); - return f(map); + return f(item.toObject()); }); }; root.findLinkByRel = function(rel) { @@ -41,6 +41,12 @@ function fromObject(root) { return rel === link.rel; }); } + root.isCollection = function() { + return typeof this.collection == 'object'; + } + root.isTemplate = function() { + return typeof this.template == 'object'; + } return root; } -- cgit v1.2.3