如何将默认错误处理程序添加到所有骨干机型? [英] How to add a default error handler to all Backbone models?

查看:101
本文介绍了如何将默认错误处理程序添加到所有骨干机型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

主干模型提供注册一个回退错误处理程序将每对服务器的调用失败,并没有提供任何具体的处理程序的时间称为一个选项。

Backbone model provides an option to register a fallback error handler that will be called each time a call to the server fails and no specific handler is provided.

MyModel = new Backbone.Model.extend({
  initialize: function(option) {
    this.on("error", this.fallbackErrorHandler);
  },

  fallbackErrorHandler: function() {
    // default behavior in case of an error
  },

  foo: function() {
    this.fetch();             // fallbackErrorHandler will be called
    this.fetch({              // the inline error handler will be called
      error: function() {
        // some specific code for this error
      }
    });
  }    
});

我想要做什么:

我想在我的应用程序中的所有骨干机型有一个备用的错误处理程序。
但是 - 我不想明确添加的处理程序为每个模型。
我想定义和注册一次的处理程序,并把它应用到每个骨干模型在我的应用
此外, 我不想改变现有的code ,我不希望改变开发商在我的应用程序定义模型的方式。

What I want to do:

I would like all the backbone models in my application to have a fallback error handler. But - I don't want to explicitly add the handler for each model. I would like to define and register the handler once and have it applied to each backbone model in my application. In addition, I don't want to change existing code and I don't want to change the way developers define Models in my application.

我添加了注册一个处理程序的初始化中的 Backbone.Model.prototype 的对象。

I added an initializer which registers a handler on the Backbone.Model.prototype object.

App.Marionette.addInitializer(function() {
  Backbone.Model.prototype.on("error",function(){
     // default behavior in case of an error
  });
});

这code工作,我的回退的错误处理程序被调用。
然而,这方code的影响是,在应用程序中的任何机型骨干注册的所有错误处理被登记在的 Backbone.Model.prototype 的,而不是对的 Backbone.Model 的。所以,很显然,这不是一个好的解决方案。

This code worked and my fallback error handler was called. However, the side affect of this code was that all error handlers registered in any backbone models in the application were registered on the Backbone.Model.prototype instead of on the Backbone.Model. So obviously this is not a good solution.

我也想过定义的为MyModel 的扩展了的 Backbone.Model 的并注册的默认处理程序。在我的应用程序的所有其他车型那么将延长的为MyModel 的,而不是直接延长Backbone.Model。
不过,我想使这个无缝地,而无需改变现有的code,然后指示所有的开发人员扩展的为MyModel 的而不是 Backbone.Model

I also thought about defining MyModel which extends the Backbone.Model and registers the default handler. All other models in my application will then extend MyModel instead of directly extending Backbone.Model. However, I'm trying to make this seamlessly without having to change existing code and then instructing all the developer to extend MyModel instead of Backbone.Model.

有没有人有这个问题的解决方案?

推荐答案

骨干默认使用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);
};

这篇关于如何将默认错误处理程序添加到所有骨干机型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆