Backbone.js:组合多个模型的复杂视图 [英] Backbone.js: complex views combining multiple models

查看:22
本文介绍了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})

我仍然更喜欢第一种的复合方法,因为它不会混淆模型是什么.

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

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

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