我正在閱讀構建酒窖應用程序的Backbone教程http://coenraets.org/blog/2011/12/backbone-js-wine-cellar-tutorial-part-1-getting-started/。本教程的作者沒有清楚地解釋一點,我無法從文檔中找出它。也就是說,使用this.model.models
,你在看呈現以下骨幹:使用「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});
和收集聲明葡萄酒作爲其模型
window.WineCollection = Backbone.Collection.extend({
model:Wine,
url:"../api/wines"
});
所以,當WineListView被實例化時,它實際上是this.model
Wine List Collection。而且,從文檔,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
?從本質上再次獲得收藏?
嗯。當視圖查看集合而不是單個對象時,主幹視圖具有'collection'屬性,應該使用'collection'屬性來代替'model'。 [其中一個評論](http://coenraets.org/blog/2011/12/backbone-js-wine-cellar-tutorial-part-1-getting-started/#comment-27575)確實注意到這個問題,但在那裏沒有來自該教程的作者的迴應。 [Derick Bailey的](http://stackoverflow.com/users/93448/derick-bailey)博客帖子可能是一個更好的學習這個東西的地方。 – 2012-04-17 21:08:09
@ muistooshort好的,謝謝,我沒有看到那個評論.. – Leahcim 2012-04-18 01:28:03