1
我有一個集合,其中的數據返回看起來像:不知道如何使用模板中的骨幹陣列的對象,並強調模板
{
"departments": ["Customer Support", "Marketing"],
"classes": ["Planning", "Drawing"]
}
我不是太清楚如何使用下劃線模板循環到每個輸出的部門,現在我正在使用._each
,但我的輸出是object Object
。任何人都可以建議如何解決這個問題?
小提琴:http://jsfiddle.net/kyllle/aatc70Lo/7/
模板
<script type="text/template" class="js-department">
<select>
<% _.each(departments, function(department) { %>
<option value="<% department %>"><% department %></option>
<% }) %>
</select>
</script>
JS
var Department = Backbone.Model.extend();
var Departments = Backbone.Collection.extend({
model: Department,
parse: function(response) {
return response;
}
});
var DepartmentView = Backbone.View.extend({
template: '.js-department',
initialize: function() {
console.log('DepartmentView::initialize', this.collection.toJSON());
},
render: function() {
this.$el.html(this.template(this.collection.toJSON()));
}
});
var departments = new Departments({
"departments": ["Customer Support", "Marketing"]
}, {parse:true});
var departmentView = new DepartmentView({
collection: departments
});
document.body.innerHTML = departmentView;