2012-07-13 89 views
2

我正在使用骨幹路由器來處理客戶端單擊單個頁面上的各種選項。除此之外,該路由器的行爲與簡單的同頁錨鏈接標籤類似。有沒有辦法強制骨幹重新路由到當前的URL?

是我遇到的問題是,如果用戶點擊其中的一個選項(比如,「詳細信息」),然後滾動走,他們可能需要再次點擊「詳細信息」。如果他們這樣做了,沒有任何反應 - 應用程序已經轉向了細節並且不會重新路由。我只是使用簡單的鏈接,比如<a href="#details">Details</a>,但還有更多的事情不只是在頁面上跳轉。有沒有辦法強制重新路由發生?

回答

-1

如果您在視圖中綁定了單擊事件,則可以手動觸發路線。

View.js

SearchView = Backbone.View.extend({ 
    initialize: function(options){ 
    alert("Alerts suck."); 
    this.router = options.router; 
    }, 
    events : { 
    "click a.detail_link" : "showDetails" 
    }, 
    showDetails : function(){ 
    this.router.navigate("/#details", true); 
    } 
}); 
2

它將always return,沒有參數,迫使它。我現在正在尋找解決方案,我看到的是以新的方式靜靜地取代當前路線,然後嘗試導航到較舊的路線。

UPD:實際上,you can use Backbone.history.loadUrl

4

號樓關Alexey's answer,這可能迫使骨幹的歷史/路由器「改道」當前的URL,就好像它觸發重新加載。

如果navigate的典型調用由於已經位於同一個url上而失敗/返回任何內容,並且在這些情況下調用loadUrl來強制執行,您實際上可以調用detect

例如,我的整個網站的內部鏈接的處理程序是這樣的:

// `Backbone.history.navigate` is sufficient for all Routers and will 
// trigger the correct events. The Router's internal `navigate` method 
// calls this anyways. 
var ret = Backbone.history.navigate(href, true); 

// Typically Backbone's history/router will do nothing when trying to load the same URL. 
// In some cases (links with .allow-reload), we want it to re-fire the same route. 
// We can detect when Backbone.history.navigate did nothing, and force the route. 
if (ret === undefined && $link.hasClass('allow-reload')) { 
    Backbone.history.loadUrl(href); 
}