【发布时间】:2014-12-01 14:31:10
【问题描述】:
我遍历一个集合,试图在每个循环中向表中添加一行。这是循环集合并构建单个视图的代码,
App.Views.OrganisationMembersTab = Backbone.View.extend({
el: '#members',
template: _.template( $('#tpl-members-tab-panel').html() ),
events: {
},
initialize: function() {
this.$el.html( this.template() );
this.render();
},
render: function() {
this.addAll();
},
addAll: function() {
this.collection.each( this.addOne, this);
},
addOne: function(model) {
console.log(model);
var tableRow = new App.Views.OrganisationsMemberRow({
model: model
});
tableRow.render();
}
});
被调用来构建行的单个视图如下所示,
App.Views.OrganisationsMemberRow = Backbone.View.extend({
el: '.members-list tbody',
template: _.template($('#tpl-organisation-member-row').html() ),
events: {
},
initialize: function() {
},
render: function() {
this.$el.prepend( this.template({
member: this.model.toJSON()
}));
return this;
}
});
使用toJSON() 将其解析为 JSON 后使用的模型如下所示,
email: "john.doe@email.com"
first_name: "John"
last_name: "Doe"
该行的模板如下所示,
<script type="text/template" id="tpl-members-tab-panel">
<table class="table table-striped members-list">
<thead>
<tr>
<th>A</th>
<th>B</th>
<th>C</th>
<th>D</th>
</tr>
</thead>
<tbody>
<tr>
<td colspan="4"><button class="btn btn-success btn-sm pull-right">Add +</button></td>
</tr>
</tbody>
</table>
</script>
上面构建了主要的表格组件,下一个模板实际上是针对一个数据行的。
<script type="text/template" id="tpl-organisation-member-row">
<tr>
<td>#</td>
<td><%= first_name %> <%= last_name %></td>
<td>Admin <input type="checkbox" /></td>
<td>Remove</td>
</tr>
</script>
我在主表中得到所有输出,然后在主表中我得到任何前置或空的 <tr> 这是为什么?
【问题讨论】:
标签: javascript html backbone.js backbone-views backbone.js-collections