Backbone.js的:结合多种模型复合的看法 [英] Backbone.js: complex views combining multiple models

查看:101
本文介绍了Backbone.js的:结合多种模型复合的看法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,所有测试和教程,我一直很努力,获得结构在我的脑海,让我看到一个视图绑定到1模式。

So far all tests and tutorials i've been trying, to get the structure in my head, show me that a view is bound to 1 model.

可以说,我有一个小的应用程序来存储和管理联系方式(地址簿)
我有一个可以登录多个用户
他们每个人都有自己的联系人集合。

lets say i have a small app to store and manage contact details (an address book) i have multiple users that can login and they each have their own collection of contacts.

用户详细信息视图将被绑定到用户模型
同样适用于接触

the user detail view would be bound to the user model same goes for contacts

但说我想显示网格结合这两个
X轴显示应用程序的所有联系人
Y轴显示的所有用户,

but say i would like to show a grid combining those two X axis showing all contacts in the app Y axis showing all users,

如何工作的呢?我需要为此创造一个新的模型,绑定到一个新的看法?

你的想法,它只是一个理论上的例子我没有构建应用程序,但其获得具有结合多个模型视图的想法

you get the idea, its just a theoretical example i am not building that app but its to get the idea of having a view combining multiple models

推荐答案

在这种情况下,我会考虑具有您的子模型的动态模型。在你的路由处理,你可以做这样的事情:

In that case, I'd consider a dynamic model with both of your sub-models. In your route handler, you could do something like this:

var model = new Backbone.Model();
model.set({contacts: new ContactsModel(), users: new UsersModel()});
var view = new GridView({model: model});

当然,没有什么距离传递模型单独阻止你:

Of course, there is nothing stopping you from passing in the models separately:

var contacts = new ContactsModel();
var users = new UsersModel();
var view = new GridView({model: contacts, users: users})

我仍然preFER第一复合的方法,因为它不合并的模式是什么。

I still prefer the composite approach of the first because it doesn't conflate what the model is.

这篇关于Backbone.js的:结合多种模型复合的看法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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