【发布时间】:2016-10-16 16:33:40
【问题描述】:
我有一组具有不同属性的模型,我需要在<select> 标记内渲染其中一些模型,每个模型都作为<option>。呈现此集合的视图嵌套在另一个视图中。这是我的收藏:
var UserCollection = Backbone.Collection.extend({
url: 'http://localhost:3000',
developers: function () {
var developers = this.where({role: "developer"});
return new UserCollection(developers);
}
});
这是我对select 标签的看法:
var InterviewersView = Backbone.View.extend({
initialize: function () {
this.template = _.template(interviewersTemplate);
this.collection = new UserCollection();
this.collection.fetch();
this.interviewers = this.collection.developers();
},
render: function () {
this.$el.html(this.template());
return this;
}
});
这是我的模板:
<label>Interviewer</label>
<select class="form-control" id="js-interviewer-selector">
<% _.each(this.interviewers.toJSON(), function(interviewer) { %>
<option value="<%= interviewer.login %>">
<%= interviewer.firstName %> <%= interviewer.lastName %>
</option>
<% }) %>
</select>
模板在另一个视图中正确且完全按照我的需要呈现,但 select 标记内没有选项,它是空的。我做错了什么?
【问题讨论】:
-
尝试添加分号;在每个方法的末尾。
-
@mahendrapratapjewaria 我已经查看了那个问题和答案,但不幸的是这还不够有用
标签: javascript jquery backbone.js