【发布时间】:2013-07-07 17:31:15
【问题描述】:
我正在尝试将集合视图嵌套到模型视图中。
为此,我使用了 Backbone 的 Marionnette Composite View 和 followed that tutorial
最后他像这样初始化嵌套的集合视图:
MyApp.addInitializer(function(options){
var heroes = new Heroes(options.heroes);
// each hero's villains must be a backbone collection
// we initialize them here
heroes.each(function(hero){
var villains = hero.get('villains');
var villainCollection = new Villains(villains);
hero.set('villains', villainCollection);
});
// edited for brevity
});
如果不使用 Marionette 的 addInitalizer,您将如何做同样的事情?
在我的项目中,我正在从服务器获取数据。当我尝试做类似的事情时:
App.candidatures = new App.Collections.Candidatures;
App.candidatures.fetch({reset: true}).done(function() {
App.candidatures.each(function(candidature) {
var contacts = candidature.get('contacts');
var contactCollection = new App.Collections.Contacts(contacts);
candidature.set('contacts', contactCollection);
});
new App.Views.App({collection: App.candidatures});
});
我从集合中得到一个“未定义的选项”:
App.Collections.Contacts = Backbone.Collection.extend({
model: App.Models.Contact,
initialize:function(models, options) {
this.candidature = options.candidature;
},
url:function() {
return this.candidature.url() + "/contacts";
}
)};
【问题讨论】:
标签: backbone.js initialization marionette