有没有办法强制骨干重新路由到当前的URL?

时间:2012-07-13 17:20:50

标签: javascript backbone.js anchor

我正在使用骨干路由器来处理客户端点击单个页面上的各种选项。除此之外,此路由器的行为与简单的同页锚标签链接类似。

我遇到的问题是,如果用户点击其中一个选项(例如“详细信息”)然后滚动,他们可能想再次点击“详细信息”。如果他们这样做,没有任何反应 - 应用程序已经路由到详细信息,不会重新路由。我只会使用简单的链接,例如<a href="#details">Details</a>,但是除了跳转页面之外还有更多的链接。有没有办法强制重新路由发生?

3 个答案:

答案 0 :(得分:4)

建立Alexey's answer,可以强制Backbone的历史/路由器“重新路由”当前的URL,就好像它会触发重新加载一样。

如果对navigate的典型调用失败/实际上只返回由于已存在于同一网址上的内容,则实际上detect可以{{3}},并且在这些情况下调用loadUrl强制它。

例如,我的站点范围内部链接处理程序如下所示:

// `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);
}

答案 1 :(得分:2)

它将always return,没有强制它的参数。我现在正在寻找解决方案,我看到的是用新的方式静默替换当前路线,然后尝试导航到旧路线。

UPD:实际上,you can use Backbone.history.loadUrl

答案 2 :(得分:-1)

如果在视图中绑定click事件,则可以手动触发路径。

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);
  }
});
相关问题