我有一个具有不同属性的模型集合,我需要在<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
标签内没有选项,它是空的。我究竟做错了什么?
尝试添加分号;在每个方法的末尾。 – Mahi
http://stackoverflow.com/questions/9154628/rendering-backbone-js-collection-as-a-select-list – Mahi
@mahendrapratapjewaria我已经看过这个问题和答案,但不幸的是,这并没有帮助足够的 – AlexNikolaev94