Backbone: multiple View Models for the same model

前端 未结 4 1218
暗喜
暗喜 2021-02-05 23:25

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

4条回答
  •  梦如初夏
    2021-02-05 23:32

    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;
                           },
    
         
    

提交回复
热议问题