Backbone.js 在回退中哈希后斜线 - 历史推送状态

发布于 2024-12-20 17:46:05 字数 389 浏览 0 评论 0原文

我正在使用 Backbone.js 的路由。 它为这样的浏览器生成网址:

http://my-app.com/help

对于 Internet Explorer(IE10 除外)和旧的非 HTML5 浏览器:

http://my-app.com/#help

如何配置 Backbone.js 以生成带有附加斜杠的后备网址,如下所示?:

http://my-app.com/#/help

I'm using Backbone.js's routing.
It generate urls for browsers like this:

http://my-app.com/help

For Internet Explorers (except IE10) and old non-HTML5 browsers:

http://my-app.com/#help

How to configure Backbone.js to generate fallback urls with additional slash, like this?:

http://my-app.com/#/help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

无需解释 2024-12-27 17:46:05

我知道这有点旧,但由于接受的答案不再适用于较新版本的 Backbone,我想我会分享我的发现。

我找到了一种方法让它在 IE8 和 Chrome 中工作(没有测试任何其他浏览器) - 如果您使用 Backbone.history 进行导航。

如果您在导航调用中使用两个前面的斜杠,它将创建您想要的 Url。

Backbone.history.navigate('//help');

我根本没有改变路线 - 它们不以斜线开头。在那里放一条斜线似乎可以打破它。

我还应该指出,我正在使用带有 Backbone 的 Marionette,因为这也许会有所作为。

我希望这对某人有帮助。

I know this is a bit old, but since the accepted answer no longer works in newer versions of Backbone, I figured I would share my findings.

I found a way to get this to work in IE8 and Chrome (have not testing any other browsers) - if you are using Backbone.history for navigation.

If you use two preceding slashes in the navigate call, it will create the Url like you want it.

Backbone.history.navigate('//help');

I did not change the routes at all - they do not start with a slash. Putting a slash there seemed to break it.

I should also note that I am using Marionette with Backbone as perhaps that could make a difference.

I hope this helps someone.

迷乱花海 2024-12-27 17:46:05

我确实相信您的第二个代码块与第三个代码块完全不同。前面的斜杠是在属性名称上设置的。

routes: {
    "help":                 "help",    // #help
    "search/:query":        "search",  // #search/kiwis
    "search/:query/p:page": "search"   // #search/kiwis/p7
  }

不同于:

routes: {
    "/help":                 "help",    // #/help
    "/search/:query":        "search",  // #/search/kiwis
    "/search/:query/p:page": "search"   // #/search/kiwis/p7
  }

I do believe that your 2nd code block is entirely different than the 3rd. The preceding slash is set on the property name.

routes: {
    "help":                 "help",    // #help
    "search/:query":        "search",  // #search/kiwis
    "search/:query/p:page": "search"   // #search/kiwis/p7
  }

is different than:

routes: {
    "/help":                 "help",    // #/help
    "/search/:query":        "search",  // #/search/kiwis
    "/search/:query/p:page": "search"   // #/search/kiwis/p7
  }
锦爱 2024-12-27 17:46:05

这是一个丑陋的解决方案:覆盖 getHash() 。这是原始代码:

getHash: function(windowOverride) {
  var loc = windowOverride ? windowOverride.location : window.location;
  var match = loc.href.match(/#(.*)$/);
  return match ? match[1] : '';
},

在正则表达式中添加额外的斜杠后,它似乎可以工作:

Backbone.History.prototype.getHash = function(windowOverride) {
  var loc = windowOverride ? windowOverride.location : window.location;
  var match = loc.href.match(/#\/(.*)$/);
  return match ? match[1] : '';
}

在此之后导航可能无法工作,但它可能可以通过类似的黑客修复。

Here's an ugly hack to get around it: override getHash(). Here's the original code:

getHash: function(windowOverride) {
  var loc = windowOverride ? windowOverride.location : window.location;
  var match = loc.href.match(/#(.*)$/);
  return match ? match[1] : '';
},

It seems to work after adding an extra slash to the regexp:

Backbone.History.prototype.getHash = function(windowOverride) {
  var loc = windowOverride ? windowOverride.location : window.location;
  var match = loc.href.match(/#\/(.*)$/);
  return match ? match[1] : '';
}

Navigate might not work after this, but it could probably be fixed with a similar hack.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文