简体   繁体   English

Backbone:同一型号的多个View Models

[英]Backbone: multiple View Models for the same model

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 like to create two views based on this model or this model's collection. 我想基于此模型或此模型的集合创建两个视图。

First view will display all items like this: 第一个视图将显示所有项目,如下所示:

<ul>
<li><h3>Item 1 Name</h3>
<p>Item 1 Description</p>
<p>Tag1, Tag2 ,Tag3</p>
</li>
.......
</ul>

Second view will display a list of tags and count of tagged items like this: 第二个视图将显示标签列表和标记项目的计数,如下所示:

<ul>
<li>Tag1<span>{count of items tagged with tag1}</span></li>
<li>Tag2<span>{count of items tagged with tag2}</span></li>
<li>Tag3<span>{count of items tagged with tag3}</span></li>
</ul>

I built the model, collection and view to support the first view. 我构建了模型,集合和视图以支持第一个视图。 I would like to know the right way to use the existing model (or create a new model?) to build the second view. 我想知道使用现有模型(或创建新模型?)的正确方法来构建第二个视图。

Thanks in advance... 提前致谢...

existing Item model and collection (ripped from Todo.js example) 现有项目模型和集合(从Todo.js示例中删除)

window.Item = Backbone.Model.extend({
// Default attributes for a todo item.
defaults: function() {
return {
order: Items.nextOrder()
};
}
});


window.ItemList = Backbone.Collection.extend({

model: Item,

localStorage: new Store("items"),

nextOrder: function() {
  if (!this.length) return 1;
  return this.last().get('order') + 1;
},

comparator: function(item) {
  return item.get('order');
}

});

UPDATE: Even though the overriding the parse() method works when displaying Tag names with item count, I was unable to refresh tag name/item count list after adding a new item. 更新:即使覆盖parse()方法在显示带有项目计数的标记名称时,我也无法在添加新项目后刷新标记名称/项目计数列表。 This may be due to the fact that the views are rendering from different collections. 这可能是由于视图是从不同的集合呈现的。 I will try extending the ItemList collection and overriding the parse() method. 我将尝试扩展ItemList集合并覆盖parse()方法。 Any help is greatly appreciated. 任何帮助是极大的赞赏。

@machineghost is right on; @machineghost是对的; The models are totally decoupled from the views so you can make as many views attached to the same model as you please. 模型完全与视图分离,因此您可以根据需要将相同模型的视图添加到多个视图中。 You could also extend a view, if they have logic or attributes you would like to share. 如果它们具有您想要共享的逻辑或属性,您还可以扩展视图。 When I use Backbone I often find myself extending a parent view just to override the render function, or to provide a different template. 当我使用Backbone时,我经常发现自己扩展父视图只是为了覆盖render功能,或提供不同的模板。

First view 第一种观点

ShoppingCartView = Backbone.View.extend({
  model: ShoppingCart
  ...
});

Second independent view 第二个独立观点

CheckoutView = Backbone.View.extend({
  model: ShoppingCart
  ...
});

Second view extends first 第二种观点首先延伸

CheckoutView = ShoppingCartView.extend({ 
  template: a_different_template // syntax depends on your templating framework
  ... 
});

I'm pretty new to Backbone.js myself, so take this answer with a grain of salt, but I think ... you just make the second view. 我自己对Backbone.js很陌生,所以请耐心等待这个答案,但我想......你只是做出第二个观点。 The whole point of de-coupling models and views is to make it so that you don't need to do anything to the models if all you want to do is do something different with your views. 解耦模型和视图的重点在于,如果你想做的只是做一些与你的观点不同的事情,你就不需要对模型做任何事情。

So, I think you just need to create YourView2, tell it to use the same model as YourView1 and you should be in business. 因此,我认为您只需创建YourView2,告诉它使用与YourView1相同的模型,您应该开展业务。

Paul Yoder from Backbone.js google group provided the solution. 来自Backbone.js google group的Paul Yoder提供了解决方案。 You can view it here 你可以在这里查看

After a little research, I found Collection.Parse method that seems to be right place for transforming the response after a fetch() operation. 经过一番研究,我发现Collection.Parse方法似乎是在fetch()操作后转换响应的正确位置。 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. 在Chrome中成功测试过。 Feel free to suggest improvements 随意提出改进建议

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

     </snip>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM