2015-08-25 31 views
0

如果我直接從網址欄中點擊頁面,我的本地Backbone應用程序部署(以及實際測試部署中的500)出現401錯誤。當我請求基本路線並且好奇地或者當我點擊一個將抓取頁面的按鈕時,我沒有問題。 該視圖是列出項目集合的數組。 我希望用戶能夠刷新頁面。只有當我直接點擊頁面時纔會出現401錯誤

我是Backbone的新手,所以我很難高效地進行調試。

我用$(document).ajaxErrorif (xhr.status == 401)嵌套在裏面被調用。但我不想調用登錄​​路由,因爲目標只是刷新頁面。

下面是一些代碼:

var MainRouter = Backbone.Router.extend({ 
    initialize: function() { 
    return this.bind("all", this.change); 
    }, 
    routes: { 
    "": "main", 
    "users": "users", 
    "user/:id": "user", 
    "stores": "stores", 
    "store/add": "addStore", 
    "store/edit/:id": "editStore", 
    "store/:id": "store" 
    }, 
    dashboard: function() { 
    return ROOT.VIEW = new MainView(); 
    }, 
    stores: function() { 
    return ROOT.VIEW = new StoresView({ 
     collection: new Stores() 
    }); 
    }, 
    store: function(id) { 
    return ROOT.VIEW = new StoreView({ 
     model: new Store({ 
     id: id 
     }) 
    }); 
    } 
}); 


$((function(_this) { 
    return function() { 
    _this.USER = new Administrator(); 
    _this.MAIN = new MainView({ 
     model: _this.LOGIN = new LoginState() 
    }); 
    $('#view').html(_this.MAIN.render().el); 
    _this.App = new MainRouter(); 
    Backbone.history.start({ 
     pushState: true 
    }); 
    return $.ajaxSetup({ 
     statusCode: { 
     401: function() { 
      localStorage.setItem('apiKey', null); 
      return location.reload(); 
     } 
     } 
    }); 
    }; 
})(this)); 

任何建議或意見會更受歡迎。

謝謝!

+0

分享一些代碼可能路由器和相關的意見。 –

回答

0

它看起來像登錄回調函數在過程中調用太遲,也就是說在請求之後,當然失敗了。我用jQuery的承諾解決了這個問題,並且通過使用Backbone.history.location.pathname來檢查登錄狀態和當前路由。如果用戶沒有登錄並嘗試直接連接到頁面,我只是將他重定向到項目的根目錄,因爲此應用程序中只有一個入口點。

相關問題