2

我想创建一个全局错误处理 Backbone.js 视图。此视图应该能够处理所有服务器错误并通过在 UI 上显示适当的消息来做出反应。任何人都可以帮助如何做到这一点?

4

1 回答 1

1
var NotificationView = Backbone.view.extend({

initialize: function () {
    //add your error dialogue window to body.

    $(document).ajaxComplete(function (event, xhr, settings) {

        try {
                //if you want to send error message from server side.
            if(xhr.status === 500) {
                result = $.parseJSON(xhr.responseText);
                this.show(result.message);
            }
            else if (xhr.status !== 200) { //sucess
                this.show("Something is not right !!!");
            }

            if(xhr.status === 200) {
                //success           
            }
        } catch(error) {}           
    });
},

show: function(message) {
   //show your dialogue with message.
},

hide: function() {
   //hide dialogue.
}
});

var notify = new NotificationView();
于 2013-10-16T06:44:43.263 回答