2013-10-16 64 views
2

我想創建一個全局錯誤處理Backbone.js視圖。該視圖應該能夠處理所有服務器錯誤,並通過在UI上顯示適當的消息來作出反應。任何人都可以幫助如何做到這一點?創建全局Backbone.js錯誤視圖

+0

我建議,覆蓋您Backbone.sync,並確保所有服務器的請求將通過解析錯誤掃描得到。當您發現錯誤時觸發自定義事件,並具有消息堆棧視圖以捕獲這些事件並呈現這些錯誤。 –

回答

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();