私は次のように宣言して、プラグインを使用していBackbone-nested
ます: https://github.com/afeld/backbone-nestedModels
Backbone.NestedModel.extend()
これにより、次のようにデータを取得できます。this.model.get('tasks.title')
私が抱えている問題は、ネストされた各アイテムをレンダリングすることです。を使用して 1 を出力できますthis.model.get('tasks[0].title')
。
ただし、入れ子になったそれぞれを反復処理するループを作成すると、が に追加されるメソッドの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);
}
});
データ (A プロジェクト)
[
{
"_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"
}
]
}
]