我现在用的是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"
}
]
}
]