Newbie backbone question:
Context: Building a shopping list with backbone
I have a model class called with name, description and tags (array) properties. I would
After a little research, I found Collection.Parse method that seems to be right place for transforming the response after a fetch() operation. Looks like I will need a new set model, collection and view objects. This is how I implemented the parse function in my collection object. Tested successfully in Chrome. Feel free to suggest improvements
parse: function(response) {
var items = response; //the raw item model returned from localStorage
var tagNameItemCount = [];
var selectedTags = ["Tag1", "Tag2", "Tag3"];
for(i = 0; i < selectedTags.length; i++){
var currentTagName = selectedTags[i];
var currentItemCount = this.getItemsCountByTagName(currentTagName, items);
tagNameItemCount.push({tagName: currentTagName, tagCount: currentItemCount});
}
return tagNameItemCount;
},
getItemsCountByTagName: function (tagName, items) {
var taggedItems = _.filter(items, function(item){ return _.include(item.tags, tagName); });
return taggedItems.length;
},