Backbone.js 在回退中哈希后斜线 - 历史推送状态
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我知道这有点旧,但由于接受的答案不再适用于较新版本的 Backbone,我想我会分享我的发现。
我找到了一种方法让它在 IE8 和 Chrome 中工作(没有测试任何其他浏览器) - 如果您使用 Backbone.history 进行导航。
如果您在导航调用中使用两个前面的斜杠,它将创建您想要的 Url。
我根本没有改变路线 - 它们不以斜线开头。在那里放一条斜线似乎可以打破它。
我还应该指出,我正在使用带有 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.
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.
我确实相信您的第二个代码块与第三个代码块完全不同。前面的斜杠是在属性名称上设置的。
不同于:
I do believe that your 2nd code block is entirely different than the 3rd. The preceding slash is set on the property name.
is different than:
这是一个丑陋的解决方案:覆盖 getHash() 。这是原始代码:
在正则表达式中添加额外的斜杠后,它似乎可以工作:
在此之后导航可能无法工作,但它可能可以通过类似的黑客修复。
Here's an ugly hack to get around it: override getHash(). Here's the original code:
It seems to work after adding an extra slash to the regexp:
Navigate might not work after this, but it could probably be fixed with a similar hack.