javascript 用于backbone.js ajax 请求的全局错误处理程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6150514/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 19:40:48  来源:igfitidea点击:

Global error handler for backbone.js ajax requests

javascriptrestbackbone.js

提问by Plastic Rabbit

Is there way to bind one error handler for ajax requests that performs by backbone.js?

有没有办法为backbone.js 执行的ajax 请求绑定一个错误处理程序?

My situation: I can get 401 (Unauthorized) at any time, so I need to show login popup.

我的情况:我可以随时得到 401(未经授权),所以我需要显示登录弹出窗口。

采纳答案by Julien

Use jQuery directly for this.

为此直接使用 jQuery。

$(document).ajaxError(function (e, xhr, options) {
  // do your stuff
});

You can do the same thing for CSRF in rails for exemple (using ajaxSend).

例如,您可以对 rails 中的 CSRF 执行相同的操作(使用 ajaxSend)。

You can read more here: http://api.jquery.com/jQuery.ajax#advanced-options

您可以在此处阅读更多信息:http: //api.jquery.com/jQuery.ajax#advanced-options

回答by Bill Eisenhauer

Backbone's sync triggers an 'error' event when errors occur. So one approach you could take is to extend Backbone's Model and Collection objects to add these add-on error checks. It would look something like this:

发生错误时,Backbone 的同步会触发“错误”事件。因此,您可以采用的一种方法是扩展 Backbone 的 Model 和 Collection 对象以添加这些附加错误检查。它看起来像这样:

ErrorHandlingModel = Backbone.Model.extend({

    initialize: function(attributes, options) {
        options || (options = {});
        this.bind("error", this.defaultErrorHandler);
        this.init && this.init(attributes, options);
    },

    defaultErrorHandler: function(model, error) {
        if (error.status == 401 || error.status == 403) {
            // trigger event or route to login here.
        }
    }

});

OtherModel = ErrorHandlingModel.extend({
});

and you would do something similar for the Collection object. I haven't tested the above, but think its pretty close. Obviously, you would choose better class names. The init method just enables subclasses to get a chance to do their own initialization.

你会对 Collection 对象做类似的事情。我没有测试过上面的,但认为它非常接近。显然,您会选择更好的类名。init 方法只是让子类有机会进行自己的初始化。

回答by Vytautas Butkus

What I found is possibly the "most right way" in Backbone:

我发现可能是 Backbone 中“最正确的方法”:

var GeneralErrorView = Backbone.View.extend({
  events: {
    'ajaxError': 'handleAjaxError'
  },
  handleAjaxError: function (event, request, settings, thrownError) {
    //...handling goes here
  }
});

this.view = GeneralErrorView({el: document});

You can put any error handling logic without extending Models or Collections. Make use of Backbone.Events inside handler and transmit messages to other error handlers or something like that.

您可以在不扩展模型或集合的情况下放置任何错误处理逻辑。在处理程序中使用 Backbone.Events 并将消息传输到其他错误处理程序或类似的东西。

回答by Henry Wilson

You can handle this via the jQuery .ajaxSetup method. We have an identical situation (backbone app which may get a 401 error at any time) and we handle it by using jQuery's ajaxSetup in the entry point of our app:

您可以通过 jQuery .ajaxSetup 方法处理此问题。我们有一个相同的情况(主干应用程序可能随时出现 401 错误),我们通过在应用程序的入口点使用 jQuery 的 ajaxSetup 来处理它:

var appView = new AppView(options);

$.ajaxSetup({
    cache: false,
    statusCode: {
        401: function () {
            appView.signIn();
        }
    }
});

appView.render();

Backbone.history.start({ root: config.rootUrl, pushState: true });

This approach gives global error handling without the need to extend the Backbone base classes.

这种方法提供了全局错误处理,而无需扩展 Backbone 基类。