我正在尝试向服务器发出 ajax 请求。我在集合上使用 fetch()
方法。它在监控 google chrome 网络选项卡后成功从服务器检索数据。
这是场景。
下面有模型
var Model = Backbone.Model.extend({
urlRoot: '/contacts',
});
集合
如下
var Collection = Backbone.Collection.extend({
model: Model,
url: '/contacts'
});
ViewOne
下面
var ViewOne = Backbone.View.extend({
initialize: function () {
var collection = new Collection().fetch(); // also tried with these options { wait:true, reset: true, async: false }
new ViewTwo({ collection: collection });
}
});
下面是ViewTwo
var ViewTwo = Backbone.View.extend({
initialize: function () {
this.collection.each(this.render, this); // Uncaught TypeError: undefined is not a function
},
render: function () {
console.log(this.model);
}
});
如您所见,我遇到了 Uncaught TypeError: undefined is not a function
错误。
this.render
存在所以这不是问题。问题是 this.collection
上的每个函数。 我该如何解决这个问题?
最佳答案
简单的在fetch成功回调中创建新 View
var ViewOne = Backbone.View.extend({
initialize: function () {
var collection = new Collection();
collection.fetch({
success:function(){
new ViewTwo({ collection: collection });
}
});
}
});
关于javascript - each() 函数未在 this.collection 上定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26626032/