我的收藏是空的,但包含我的数组。我想在集合上执行 where ,但这是不可能的,因为 d 模型返回一个数组 [0]。所以数据不会被推送到那里。
我想这是因为我的数据是异步生成的
d {models: Array[0], length: 0, _byId: Object, constructor: function, model: function…} _byId: Object _events: Object _idAttr: "id" _listenerId: "l4" length: 15 models: Array[15 ] 原型:e
来自 app.js
var AppRouter = Backbone.Router.extend({
routes: {
"Onderwerpen": "subjectsList"
},
initialize: function () {
this.categories = new Categories();
this.categories.getCategoriesFromJSON();
console.log(this.categories);
//view for subjectsList
this.subjectsListView = new SubjectsListView({collection: this.categories});
},
subjectsList: function () {
$('#app').html(this.subjectsListView.render().el);
},
});
var app = new AppRouter();
$(function() {
Backbone.history.start();
});
来自 category.js
var Categories = Backbone.Collection.extend({
model : Category,
getCategoriesFromJSON : function(){
var self = this,
categories = [];
$.ajax({
url : './catalogue.json',
dataType : 'json'
}).done(function(data){
console.log('Catalogue retrieved');
_.each(data.categories, function( categorieObj ){
var categorieName = _.keys(categorieObj)[0],
categorieAttributes = categorieObj[categorieName];
categories.push( categorieAttributes );
});
//console.log(JSON.stringify(categories));
self.add( categories );
self.trigger('reset');
});
}
});
怎么了?