骨干嵌套:渲染嵌套元素
问题描述:
我现在用的是Backbone-nested
插件,宣告我的Models
为Backbone.NestedModel.extend()
:https://github.com/afeld/backbone-nested骨干嵌套:渲染嵌套元素
这让我找回数据,例如:this.model.get('tasks.title')
我有正在呈现每个问题嵌套的项目。我可以使用this.model.get('tasks[0].title')
输出1。
然而,如果我写一个循环遍历每个嵌套,当return this;
在render()
方法为$el
要附加回Collection $el
后调用,它将失败。
问题
我怎么能写的嵌套元素被渲染到集合视图的有效途径?
集合视图
App.Views.Tasks = Backbone.View.extend({
el: '#taskList',
initialize: function() {
Event.on('tasks:show', this.show, this);
this.collection.on('add', this.addOne, this);
},
render: function() {
this.collection.each(this.addOne, this);
return this;
},
addOne: function(project) {
console.log(project.toJSON());
var taskView = new App.Views.Task({ model: project });
this.$el.append(taskView.render().el);
},
show: function(id) {
var project = this.collection.get(id);
var taskView = new App.Views.Task({ model: project });
this.$el.html(taskView.render().el);
}
});
的数据(一期工程)
[
{
"_id": "51497f8dc5c3e3ec28ce4571",
"name": "First Project",
"tasks": [
{
"title": "Second",
"content": "Lots of content...",
"deadline": "1-1-2011",
"status": "in-progress",
"_id": "51497f8d726694b230000002"
},
{
"title": "Third",
"content": "Lots of content...",
"deadline": "1-1-2011",
"status": "in-progress",
"_id": "51497fa18aeb50c630000002"
}
]
}
]
答
我似乎几次碰到这个问题,有关显示嵌套布局。
有一个JavaScript库位于Backbone.js之上,可以减少需要编写的锅炉板代码的数量,并且它们具有最适合嵌套集合的CompositeView。我已经包含了文档和讨论使用它的文章。
Marionette CompositeView documentation
Marionette CompositeView Article
A similar question that I answered earlier
希望这有助于。