且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将默认错误处理程序添加到所有骨干机型?

更新时间:2023-12-01 21:32:04

骨干默认使用jQuery的AJAX方法调用。您可以勾这一直接,而不是使用主干:

Backbone uses jQuery's AJAX method calls by default. You can hook in to this directly, instead of using Backbone:

http://api.jquery.com/ajaxError/

如果不给你想要什么,你可以重写取的方法在骨干模型:

If that doesn't give you what you want, you can override the "fetch" method in Backbone's model:


var originalFetch = Backbone.Model.prototype.fetch;

Backbone.Model.prototype.fetch = function(options){

  var originalError = options.error;

  options.error = function(model, error){
    if (originalError){ originalError(model, error); }

    // call your global error handler here.
    App.myGlobalErrorHandler(model, error);

  }

  originalFetch.apply(this, arguments);
};