在 Backbone.js 中组合哈希和非哈希 URL
有没有什么方法可以在 Backbone.js 应用程序中组合 hash 和 非哈希 URL?
我设置了 Backbone.history.start({pushState: true})。当用户单击某个链接时,我从服务器获取 JSON 数据,更新页面,并调用 Backbone.history.navigate 来更改浏览器中的 URL(例如来自 example.com/zlinsky/ kampan/mf/ 至 example.com/moravskoslezsky/kampan/mf/
)。
如果用户从浏览器复制 URL 并在第二个选项卡中打开,他将看到相同的页面(因此以这种方式更新的每个页面在服务器上都有相应的页面)。这正是我想要的。
但现在我遇到了问题...
页面上也有几个 更改 URL,例如更改为
example.com/moravskoslezsky/kampan/mf/#state1
(因此,当有人发送此 URL 时,另一方将看到相同的页面,与发件人处于相同的状态)。
我找不到方法,如何在 Backbone.js 中做到这一点。如果我在 Backbone.history
上设置 pushState: true
,Router
会忽略哈希标签。
如果我设置 pushState: false
,我将无法像上面第一段中所述那样设置 URL。
谢谢你的任何提示。
Is there some way how to combine hash and non-hash URLs in Backbone.js application?
I set Backbone.history.start({pushState: true})
. When user click on some link, I fetch JSON data from server, update page, and call Backbone.history.navigate
to change URL in browser from (for example from example.com/zlinsky/kampan/mf/
to example.com/moravskoslezsky/kampan/mf/
).
If user copy URL from browser and open in second tab, he will see same page (so every page updated this way have corresponding page on server). This is exactly what I want.
But now I have problem...
I have several <select>
on page too. When user change value in them, I make some dynamic changes on page (without fetching JSON from server, updates is done only on client side). I would like change URLs according to <select>
, for example to example.com/moravskoslezsky/kampan/mf/#state1
(so, when somebody send this URL, the other side will see same page, in same state as sender).
I could not find way, how to do it in Backbone.js. If I set pushState: true
on Backbone.history
, Router
ignore hash tags.
If I set pushState: false
, I am not able to set URLs like I describe in first paragraph above.
Thank you for any hint.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以致电:
Backbone.history.navigate( "/foo/bar#fragment" )
但我不认为这是一个好主意,因为 ie 不支持pushstate,所以backbone将使用hash bang urls (即)。
也许您可以使用查询字符串:
Backbone.history.navigate( "/foo/bar?foo=bar", true )
,这将在现代浏览器中使用:http://domain.tld/ foo/bar?foo=bar
并在 ie 中:http://domain.tld#/foo/bar?foo=bar
You can call:
Backbone.history.navigate( "/foo/bar#fragment" )
But i don't think it's a good idea, because ie doesn't support pushstate, so backbone is going to use hash bang urls (in ie).
Maybe you could use querystrings:
Backbone.history.navigate( "/foo/bar?foo=bar", true )
which will be in modern browsers:http://domain.tld/foo/bar?foo=bar
and in ie:http://domain.tld#/foo/bar?foo=bar