【发布时间】:2011-12-19 01:56:31
【问题描述】:
我是 Backbone.js n00b 并试图了解它。我知道如何使用视图和内置的 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"}]);
});
它在视图中的模板属性行上中断,但我不确定那是我需要查看的地方。我应该渲染一个集合还是在这里完全忽略了要点(也许集合只是对对象进行分组,我不应该将其视为可以渲染的列表)?
感谢您的帮助...
【问题讨论】:
标签: collections backbone.js render