将 Backbone.js 集合渲染为选择列表
我尝试使用 Underscore.js 模板将 Backbone.js 集合呈现为 select
列表,但该列表未填充。正在显示 select
元素,但没有 options
。
我已经确认我能够将各个属性传递到我的模板中并将它们呈现为 label
元素,因此问题一定是我如何处理该集合。
这是我的主干代码:
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>
有什么建议吗?
I'm trying to render a Backbone.js collection as a select
list using an Underscore.js template, and the list isn't getting populated. The select
element is displaying, but there are no options
.
I have confirmed that I'm able to pass individual properties into my template and render them as label
elements, so the issue must be how I'm trying to handle the collection.
Here's my Backbone code:
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();
And my template:
<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>
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你有几个不同的问题。
元素放在
rates
是一个 Backbone 集合,因此它已经可以访问 Underscore 的每个
;将其包装为_(rates)
只会混淆 Underscore 并阻止任何迭代的发生。rate
是一个 Backbone 模型实例,因此它不会有duration
属性,您必须说rate.get('duration')< /代码>。
您的模板应该看起来更像这样:
演示:http://jsfiddle.net/ambigously/AEqjn/
或者,您可以修复模板中的嵌套错误以生成有效的 HTML:
并使用
toJSON()
在您看来将原始数据提供给模板而不是集合本身:演示:http://jsfiddle.net/ambigously/VAxFW/
我认为后一个是您的目标,因为这将是使用模板的更标准方法在骨干中。
You have a couple different problems.
<option>
elements after the<select>
instead of inside it. This will produce invalid HTML and the browser will butcher that once you get anything out of your template.rates
is a Backbone collection so it already has access to Underscore'seach
; wrapping it as_(rates)
just confuses Underscore and prevents any iterations from happening.rate
is a Backbone model instance so it won't have aduration
property, you have to sayrate.get('duration')
.Your template should look more like this:
Demo: http://jsfiddle.net/ambiguous/AEqjn/
Alternatively, you can just fix the nesting error in your template to produce valid HTML:
and use
toJSON()
in your view to feed raw data to your template rather than the collection itself:Demo: http://jsfiddle.net/ambiguous/VAxFW/
I think the latter one is what you were aiming for as that would a more standard approach to working with templates in Backbone.