主要成分:同型号的多个视图模型 [英] Backbone: multiple View Models for the same model

查看:105
本文介绍了主要成分:同型号的多个视图模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

新手骨干的问题:

环境:建设有骨干购物清单

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.

在此先感谢...

现有的项目模型和收集(从Todo.js例如撕开)

existing Item model and collection (ripped from Todo.js example)

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');
}

});

更新:即使覆盖parse()方法与项目数显示标签名称时的作品,我无法添加新项目后,刷新标签名称/项目数列表。这可能是由于该视图从不同集合呈现的事实。我会尽量延长ITEMLIST收集和覆盖parse()方法。任何帮助是极大的AP preciated。

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. 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. Any help is greatly appreciated.

推荐答案

保罗·尤德从谷歌Backbone.js的集团提供的解决方案。
您可以查看这里

Paul Yoder from Backbone.js google group provided the solution. You can view it here

这篇关于主要成分:同型号的多个视图模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆