我试图将我的 json 文件 /api/tables.json 中的所有对象名称渲染到 ? <li>there</li>
的
var Table = Backbone.Model.extend({
defaults: {
name: 'table',
id: 1
}
});
var Tables = Backbone.Collection.extend({
model: Table,
url: 'api/table.json'
});
var TablesView = Backbone.View.extend({
el: '#mydiv',
template: _.template($("#table-template").html()),
initialize : function() {
this.coll = new Tables()
this.listenTo(this.coll, 'reset', this.render);
this.coll.fetch();
},
render : function() {
this.$el.html(this.template({ table: this.coll.toJSON() }));
return this;
}
});
这是我在 index.html 中的模板:
<div id="mydiv"></div>
<script type="text/template" id="table-template">
<ul>
<% _.each(table, function(table) { %>
<li><%= table.name %></li>
<% }); %>
</ul>
</script>
来自 json 文件的数据:
[
{
"name": "Table 1",
"id": 1
},
{
"name": "Table 2",
"id": 2
},
{
"name": "Table 3",
"id": 3
},
{
"name": "Table 4",
"id": 4
}
]
请帮助我....我不知道哪里有问题或缺少什么。