我是backbone.js的新手,我在给我的收藏品成功回调时遇到了一些问题。我正在重写fetch,以便在其中包含一个带参数的url。据我所知,我应该能够在我传递给Backbone.Collection.prototype.fetch.call()的选项中分配成功回调...但是,我的代码无效。 Fetch工作正常,但不调用回调函数。
以下是我的一些代码:
App.ChartController = {
load: function(userConceptId) {
App.chartPointList.fetch(userConceptId);
}
};
App.ChartPointList = Backbone.Collection.extend({
model: App.ChartPoint,
url: function() {
return '/chartpoints/' + this.userConceptId;
},
fetch: function(userConceptId, options) {
console.log("fetch chart point");
typeof(options) != 'undefined' || (options = {});
options.success = this.postProcess;
options.error = this.handleError;
this.userConceptId = userConceptId;
return Backbone.Collection.prototype.fetch.call(this, options);
},
postProcess : function (resp, status, xhr) {
console.log("postprocess"); // never gets called
/**
... whole bunch of stuff...
**/
new App.Views.ChartView({ collection: this });
},
handleError : function (resp, status, xhr) {
alert("could not load chart data!"); // also not called
}
});
知道我做错了什么吗?谢谢!
答案 0 :(得分:4)
@ fguillen的评论和another SO thread帮我解决了这个问题。具体来说:
Collection.fetch()会在成功时调用reset(),这反过来会触发'reset'事件。集合重置事件的任何订阅者都应该收到该事件。
问题不在于我的成功回调。原来我在订阅了ChartPointList重置事件的视图中遇到了问题。该视图中的一个函数在成功回调之前被称为并抛出错误,因此未调用成功回调。