我有一个用这一行获取的集合:
console.info("before fetching drivesCollection");
this.drivesCollection.fetch({
success: function() {
console.info("inside fetching drivesCollection");
},
error: function(collection, response, options) {
console.log('ERROR fetching drivesCollection ');
}
});
console.info("after fetching drivesCollection");
抓取按预期工作,错误回调永远不会被调用。之后我可以看到集合中的所有模型(200+)。问题是成功函数中的任何东西都没有被调用。我需要在那里放一个函数。
我尝试使用集合定义的 parse
函数来调试为什么 success
函数被跳过的原因,如下所示:
parse: function(response){
console.log('starting parsing of drivesCollection');
console.log(response.length);
console.log(response);
return response
},
它按预期工作,并返回插入到集合中的有效值...
知道为什么我的 success
函数永远不会运行吗?
最佳答案
我想到的两件事是:
1) 您的服务器未发送有关内容类型的正确 header 信息,快速解决方法是通过添加参数强制使用获取 JSON 数据
this.drivesCollection.fetch({
dataType: 'json',
success: function() {
console.info("inside fetching drivesCollection");
}
});
2) 或者,您错误地使用了 Backbone.Model 来获取集合而不是 Backbone.Collection(我犯过几次这个错误)
关于javascript - backbone collection fetch 不会在成功调用中运行任何东西,但可以正常获取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23437306/