【发布时间】:2015-07-11 23:00:44
【问题描述】:
我很难使用从 API 中提取的集合来初始化简单视图。当我尝试从视图中返回集合时,我尝试的所有操作都返回一个空数组。
app.models.Employee = Backbone.Model.extend({
});
app.collections.Employees = Backbone.Collection.extend({
Model: app.models.Employee,
url: "http://api.sample.com",
initialize: function(){
this.fetch();
},
parse: function(response){
console.log(response)
},
});
app.views.Container = Backbone.View.extend({
el: $('.container'),
initialize: function(){
this.collection = new app.collections.Employees();
console.log(this.collection)
var that = this;
this.collection.fetch({
success: function(){
that.render();
}
});
},
render: function(){
console.log(this.collection) //returns no models
}
});
【问题讨论】:
-
所以...看起来您可能通过将 console.log 放入解析中来阻止数据解析?尝试删除完全不必要的覆盖解析方法?
-
你确实意识到你调用了两次 fetch,对吧?
标签: backbone.js collections backbone-views