Backbonejs - 集合未定义
Backbonejs -collection is not defined
我在 backbone 中收到一个集合未定义的错误。
var CategoryList = Backbone.View.extend({
el:'#content',
render: function() {
var that = this;
var cats = new Categories();
cats.fetch({
success: function(cats) {
var template = _.template($('#category-list-template').html(), {cats: cats.models});
cats.each(function(it) {
console.log(it.toJSON());
});
that.$el.html(template);
}
})
}
});
在这个脚本中,我 运行 每个循环将数据添加到 table,但我得到一个 'cats is not defined error'。
<script type="text/template" id="category-list-template">
<table class="table striped">
<thead>
<tr>
<td>Id</td>
<td>Name</td>
</tr>
</thead>
<tbody>
<% _.each(cats, function(category) { %>
<tr>
<td><%= category.name %> </td>
</tr>
<% }); %>
</tbody>
</table>
</script>
您必须使用 toJSON
方法将集合转换为纯 javascript 对象。
var template = _.template($('#category-list-template').text());
var result = template({collection: cats.toJSON()});
that.$el.html(result);
我在 backbone 中收到一个集合未定义的错误。
var CategoryList = Backbone.View.extend({
el:'#content',
render: function() {
var that = this;
var cats = new Categories();
cats.fetch({
success: function(cats) {
var template = _.template($('#category-list-template').html(), {cats: cats.models});
cats.each(function(it) {
console.log(it.toJSON());
});
that.$el.html(template);
}
})
}
});
在这个脚本中,我 运行 每个循环将数据添加到 table,但我得到一个 'cats is not defined error'。
<script type="text/template" id="category-list-template">
<table class="table striped">
<thead>
<tr>
<td>Id</td>
<td>Name</td>
</tr>
</thead>
<tbody>
<% _.each(cats, function(category) { %>
<tr>
<td><%= category.name %> </td>
</tr>
<% }); %>
</tbody>
</table>
</script>
您必须使用 toJSON
方法将集合转换为纯 javascript 对象。
var template = _.template($('#category-list-template').text());
var result = template({collection: cats.toJSON()});
that.$el.html(result);