我是一个Backbone.js新手,正试图理解它。我知道如何使用视图和内置的underscore.js模板引擎呈现模型。现在我正在尝试呈现一个集合,这就是我卡住的地方。这里没有服务器,所以我不会远程获取任何东西,只有一个简单的带有一些JavaScript的HTML页面。
ContinentModel = Backbone.Model.extend({});
ContinentsCollection = Backbone.Collection.extend({
model: ContinentModel,
initialize: function () {
this.continentsView = new ContinentsView;
this.bind("reset", this.continentsView.render);
}
});
ContinentsView = Backbone.View.extend({
el: '#continents',
template: _.template($('#continents-template').html()),
render: function() {
var renderedContent = this.template(this.collection.toJSON());
$(this.el).html(renderedContent);
return this;
}
});
$(function() {
var continentsCollection = new ContinentsCollection();
continentsCollection.reset([{name: "Asia"}, {name: "Africa"}]);
});
在视图的模板属性行上出现了错误,但我不确定那是我需要查找的地方。我应该渲染一个集合还是完全错过了重点(也许集合只是将对象分组,我不应该将其视为可以呈现的列表)?
谢谢你的帮助...
reset
似乎没有触发视图渲染函数。有什么想法吗? - Cimm