创建Global Backbone.js错误视图

时间:2013-10-16 02:08:29

标签: javascript backbone.js

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

1 个答案:

答案 0 :(得分: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();