我正在尝试使用Underscore.js模板将一个Backbone.js集合渲染成一个select
列表,但是该列表未被填充。 select
元素正在显示,但没有options
。
我已确认能够将单个属性传递到我的模板中并将它们呈现为label
元素,因此问题必须在于我如何处理集合。
以下是我的Backbone代码:
Rate = Backbone.Model.extend({
duration : null
});
Rates = Backbone.Collection.extend({
initialize: function (model, options) {
}
});
AppView = Backbone.View.extend({
el: $('#rate-editor-container'),
initialize: function () {
this.rates = new Rates(null, { view: this } );
this.rates.add(new Rate ({ duration: "Not Set" }));
this.rates.add(new Rate ({ duration: "Weekly" }));
this.rates.add(new Rate ({ duration: "Monthly" }));
this.render();
},
render: function() {
var rate_select_template = _.template($("#rate_select_template").html(), {rates: this.rates, labelValue: 'Something' });
$('#rate-editor-container').html(rate_select_template);
},
});
var appview = new AppView();
我的模板:
<script type="text/template" id="rate_select_template">
<select id="rate-selector"></select>
<% _(rates).each(function(rate) { %>
<option value="<%= rate.duration %>"><%= rate.duration %></option>
<% }); %>
</script>
<div id="rate-editor-container"></div>
有什么建议吗?
Backbone.View
扩展中刚刚添加了this.render()
上面的代码,但好像没有任何作用。在发布问题之前,我执行了console.log(this.rates)
,输出为带有三个子对象的child
。因此,我认为我正在传递某种带有三个嵌套对象的对象,这似乎是正确的。 - Josh EarldelegateEvents
自己进行绑定(在最新源代码中查找method = _.bind(method, this);
),因此您不再需要通常的_.bindAll
操作来启动您的initialize
方法。 - mu is too short