我正在阅读关于构建酒窖应用程序的 Backbone 教程http://coenraets.org/blog/2011/12/backbone-js-wine-cellar-tutorial-part-1-getting-started/ 。教程的作者没有解释清楚一点,我无法从文档中弄清楚。即,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
实际上就是 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
已经是 wine 的集合(由于集合在视图中被声明为模型),为什么有必要这样做this.model.models
?基本上再次获得收藏?