【问题标题】:Backbone fetch success callback骨干网获取成功回调
【发布时间】:2012-06-04 17:33:19
【问题描述】:

我是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
  }
});

知道我做错了什么吗?谢谢!

【问题讨论】:

标签: backbone.js


【解决方案1】:

@fguillen 的评论和another SO thread 帮助我解决了这个问题。具体来说:

Collection.fetch() 将在成功时调用 reset(),这反过来会触发一个“重置”事件。集合重置事件的任何订阅者都应该收到该事件。

问题根本不在于我的成功回调。原来我在订阅 ChartPointList 重置事件的视图中遇到了问题。该视图中的函数在成功回调之前被调用并抛出错误,因此没有调用成功回调。

【讨论】:

  • 太棒了——听“reset”效果很好。另外this SO thread 帮助我将参数传递给特定的 fetch,而无需修改集合的 url 或 fetch。
猜你喜欢
  • 1970-01-01
  • 2013-08-16
  • 2014-03-15
  • 1970-01-01
  • 1970-01-01
  • 2015-10-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多