Global error handler for backbone.js ajax requests

前端 未结 4 2173
感动是毒
感动是毒 2020-12-23 13:02

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

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

4条回答
  •  情深已故
    2020-12-23 13:48

    What I found is possibly the "most right way" in 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.

提交回复
热议问题