ワイン セラー アプリケーションの構築に関するバックボーン チュートリアルhttp://coenraets.org/blog/2011/12/backbone-js-wine-cellar-tutorial-part-1-getting-started/を読んでいます。チュートリアルの作成者は 1 つのポイントを明確に説明しておらず、ドキュメントからも理解できません。つまり、this.model.models
以下のレンダー関数ビューに表示される の使用
window.WineListView = Backbone.View.extend({
tagName:'ul',
initialize:function () {
this.model.bind("reset", this.render, this);
},
render:function (eventName) {
_.each(this.model.models, function (wine) {
$(this.el).append(new WineListItemView({model:wine}).render().el);
}, this);
return this;
}
});
このビューのモデルは実際にはコレクションです
list:function () {
this.wineList = new WineCollection();
this.wineListView = new WineListView({model:this.wineList});
そして、コレクションは Wine をそのモデルとして宣言します
window.WineCollection = Backbone.Collection.extend({
model:Wine,
url:"../api/wines"
});
そのため、WineListView がインスタンス化されると、this.model
実際にはワイン リスト コレクションになります。また、ドキュメントから、models
コレクション内のモデルの配列へのアクセスを提供します
modelscollection.models
Raw access to the JavaScript array of models inside of the collection. Usually you'll want to use get, at, or the Underscore methods to access model objects, but occasionally a direct reference to the array is desired.
this.model
がすでにワインのコレクションである場合(コレクションがビューでモデルとして宣言されているため)、なぜそれを行う必要があるのthis.model.models
でしょうか? 本質的にコレクションを再び取得するには?