From 2db98614dde6c0e8ede6aa828132a78839c99eec Mon Sep 17 00:00:00 2001 From: Daniel Zurawski Date: Mon, 6 Jul 2015 10:19:06 +0100 Subject: [PATCH] Add .map to list facet as its needed frequently enough --- lib/components/c_facets/List.js | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/lib/components/c_facets/List.js b/lib/components/c_facets/List.js index 209e820..873b444 100644 --- a/lib/components/c_facets/List.js +++ b/lib/components/c_facets/List.js @@ -41,6 +41,7 @@ _.extendProto(List, { removeItem: List$removeItem, extractItem: List$extractItem, each: List$each, + map: List$map, _setItem: List$_setItem, _removeItem: List$_removeItem, _addItem: List$_addItem, @@ -103,8 +104,8 @@ function onChildrenBound() { item.item.setIndex(index); }); } - - // Component must have one child with an Item facet + + // Component must have one child with an Item facet if (! foundItem) throw new Error('No child component has Item facet'); this.list.itemSample = foundItem; @@ -125,7 +126,7 @@ function onChildrenBound() { function List$_createCacheTemplate() { if (!this.itemSample) return false; - + var itemSample = this.itemSample; // create item template to insert many items at once @@ -136,7 +137,7 @@ function List$_createCacheTemplate() { attr.el = itemElCopy; attr.decorate(); - var itemsTemplateStr = + var itemsTemplateStr = '{{ var i = it.count; while(i--) { }}' + itemElCopy.outerHTML + '{{ } }}'; @@ -247,7 +248,7 @@ function _updateItemsIndexes(fromIndex, toIndex) { function List$addItems(count, index) { // ,... items data var itemsData = _.slice(arguments, 2); - if (itemsData.length < count) + if (itemsData.length < count) itemsData.concat(_.repeat(count - itemsData.length, {})); var spliceArgs = [index, 0].concat(itemsData); var dataFacet = this.owner.data; @@ -348,7 +349,7 @@ function List$extractItem(index) { /** * List facet instance method * Removes item, returns the removed item that is destroyed by default. - * + * * @param {Number} index item index * @param {Boolean} doDestroyItem optional false to prevent item destruction, true by default * @return {Component} @@ -420,6 +421,14 @@ function List$each(callback, thisArg) { } +function List$map(callback, thisArg) { + return this._listItems.map(function(item, index) { + if (item) return callback.apply(this, arguments); // passes item, index to callback + else logger.warn('List$map: item', index, 'is undefined'); + }, thisArg || this); +} + + /** * Facet instance method * Destroys the list