From a3d96dd931bc9a6d6c0fba8a7bb112020ab2d394 Mon Sep 17 00:00:00 2001 From: Kieren Date: Thu, 25 Sep 2014 15:36:38 +0930 Subject: [PATCH 1/2] Update CRUDCollection outputList function outputList function accepts an (optional) options object with a single "key" property, currently this option is used to switch the collection list from and array of items to an object with the list items as keyed properties - the value of the "key" property is used to select the key values to be used as property names. However the current implementation incorrectly passes the whole listOptions object to the HyperJsonCollection (collection) function instead of just passing listOptions.key, so this feature has a bug. Additionally the listOptions.key property is later used to supply the items ID attribute name to the autoLink functionality provided by CRUDCollection. These two separate uses for the same option while similar are potentially independent, consider trying to output a list of items as an array while providing the autoLink functionality with the correct item id to generate a working _self hyperlink. This patch provides for a second option object property, "listAsKeyedObject" which is truthily tested to allow for all possible use cases to be defined by listOptions. The default behaviour of listOptions (when not supplied) is maintained with original, however the buggy behaviour of listOptions.key usage has been fixed and adjusted, so listOptions.listAsKeyedObject must be truthy for CRUDCollection to generate the items list as an object using item[listOptions.key] values as property names for each item. --- lib/CRUDCollection.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/CRUDCollection.js b/lib/CRUDCollection.js index f5571f9..853e81c 100644 --- a/lib/CRUDCollection.js +++ b/lib/CRUDCollection.js @@ -19,7 +19,17 @@ var CRUDCollection = function(options){ } var outputList = function(req, res, list, listOptions){ - var collection = res.collection(list, listOptions); + /* + * listOptions : { + * "key" : String, // item key attribute name + * "listAsKeyedObject" : Truthy // generate list as an object with items as keyed properties, requires "key" option. + * } + */ + if (!!listOptions && !!listOptions.key && !!listOptions.listAsKeyedObject) { + var collection = res.collection(list, listOptions.key); + } else { + var collection = res.collection(list); + } if (req.app.autoLink){ collection = collection.linkEach('self', function(item, name){ if (!!listOptions){ From 61e3f283c4d3d2567be35a4db74e24bc56f0ce46 Mon Sep 17 00:00:00 2001 From: Kieren Date: Thu, 25 Sep 2014 15:42:07 +0930 Subject: [PATCH 2/2] Update CRUDCollection outputList function Added check for listOptions.key existence to autoLink generating code. --- lib/CRUDCollection.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/CRUDCollection.js b/lib/CRUDCollection.js index 853e81c..05a4a39 100644 --- a/lib/CRUDCollection.js +++ b/lib/CRUDCollection.js @@ -32,7 +32,7 @@ var CRUDCollection = function(options){ } if (req.app.autoLink){ collection = collection.linkEach('self', function(item, name){ - if (!!listOptions){ + if (!!listOptions && !!listOptions.key){ return req.uri.child(item[listOptions.key]); // allow 'listOptions' to provide another key to link on. } else { return req.uri.child(name);