2012-03-22 71 views
2

我是backbone.js的新手,並有一個查詢。假設我的用戶位於http://www.foo.com/#about,然後點擊鏈接即可轉至http://www.foo.com/#profile。但我不想帶他去#profile如果他還沒有登錄。所以,如果他還沒有登錄,書籤#關於不應該改變。無法知道如何實現這一點。需要幫助。謝謝。Backbone.js - 如果訪問到一個新的書籤不會改變書籤如何

+0

我很抱歉,如果您的用戶不被允許點擊它,爲什麼您有鏈接到#profile?而不是繞過禁用鏈接,爲什麼根本不顯示它? – ggozad 2012-03-22 15:29:42

+0

說一下,點擊#profile鏈接,我會顯示一個登錄對話框,並且只有在他成功登錄後才轉到#profile。 – Sanjay 2012-03-23 03:46:40

回答

1

我建議你綁定一個click事件個人資料鏈接並添加邏輯存在,也可以使用Backbone.Router管理導航歷史。例如:

// code simplified and not tested 
App.Router = new Backbone.Router.extend({ 
    routes: { 
    "about": "about", 
    "profile": "profile", 
    }, 

    # [...] your routes functions here 
}); 

App.MenuView = Backbone.View.extend({ 
    el: "#menu", 

    events: { 
    "click #profile": "showProfile" 
    }, 

    showProfile: function(){ 
    if(App.currentUser.logged?) { 
     App.MyRouter.navigate("profile", {trigger: true}); 
    } 

    return false; 
    } 
} 
+0

謝謝。將有所幫助。任何其他模式也歡迎,因爲我正在嘗試單頁面應用程序,並會喜歡挑選最佳模式 – Sanjay 2012-03-23 03:51:21