花了很多小时,仍然无法弄清楚。sampleData.json 文件包含以下 json 对象。
[{name: "Tim", age: 5},{name: "Ida", age: 26},{name: "Rob", age: 55}]
为什么这个数据在集合对象中不可用?
var ListCollection = Backbone.Collection.extend({
url: '/sampleData.json'
});
var ListView = Backbone.View.extend({
el: $('#file-list'),
initialize: function () {
_.bindAll(this, 'render');
this.render();
},
render: function() {
$(this.collection.toJSON()).each(function(key, val) {
console.log(val['name']);
});
}
});
var list = new ListCollection();
list.fetch();
var listView = new ListView({
collection: list
});