var Text = Backbone.Model.extend({});
Texts = Backbone.Collection.extend({
model: Text,
url: '/data.json',
});
var TextsView = Backbone.View.extend({
initialize: function() {
_.bindAll(this);
this.render();
},
el: "#Texts",
template: _.template($('#TextTemplate').html()),
render: function(e){
_.each(this.model.models, function(Text){
var TextTemplate = this.template(Text.toJSON());
$(this.el).append(TextTemplate);
}, this);
return this;
}
})
var Texts = new Texts();
Texts.fetch();
var TextView = new TextsView({collection: Texts});
这给我Uncaught TypeError: Cannot read property 'models' of undefined
并且不在页面上显示任何内容。backbone:渲染此集合
在我看来,它应该是'this.collection.models'。 – dfsq
他使用Backbone,所以'this.collection'指的是他创建的'Texts'集合。这应该使循环工作,但我想你可以直接使用'this.collection.models'来访问模型。不知道。有趣。 – Gohn67
其实你是对的@dfsq。如果你使用每个方法的下划线,那么你确实需要使用'this.collection.models'。最近一定改变了。无论如何改变它使用集合上的每个方法。 – Gohn67