2016-02-04 15 views
0

我正在使用flatiron/director來做客戶端路由。使用director.js,我要爲站點根目錄定義一個默認路由,它沒有#哈希值?

我有這樣的路線:

var routes = { 
    '': function() { loadLandingPage(); }, 
    '/author': function() { author(); } 
    }; 

如果我去www.example.com#/作者,它觸發了筆者的功能。如果我訪問www.example.com#或www.example.com#/,則我的着陸頁被加載。

但是,我想爲我的網站的根沒有#的默認路由。即使我們登陸www.example.com時沒有#,我也希望着陸頁加載。

我已經瀏覽了文檔,但對於我的生活無法弄清楚。我認爲configuration: notfound方法會做我在想什麼,但它什麼也沒做。我試過如下:

var router = Router(routes).configure({ notfound: loadGlobalPage}); 

var router = Router(routes).configure({ notfound: function() {loadGlobalPage();} }); 

,也讀this thread後,我想:

router.notfound = function() { 
    loadGlobalPage(); 
}; 

我也試過無關路由器一個黑客:

function determineIfLandingPage() { 
    if (window.location.hash == "") { 
     loadGlobalPage(); 
     window.location.href += "#" 
    } 
} 

這有效,但它感覺像一個黑客。

這必須是一個有點常見的用例 - 我如何從我的路由器獲得所需的功能?

回答

2

來自文件:redirect method將解決這個問題。

route.init(['/'])將重定向到路徑/如果在URL中找不到'/#/'

+0

很好的答案。但你有一個小的錯字:)。你能編輯它嗎?它應該是route.init(['/'])。初始化中缺少't'。 –

+0

@AnteBraovic謝謝 – towry