触发同位置路线
有没有办法在backbone.js中触发相同的位置路由,例如当位置为/My/App/#/About
并且用户再次单击具有相同路由的锚点以刷新页面时内容。
Is there a way to trigger same location route in backbone.js, for example when location is /My/App/#/About
and user clicks again on anchor with the same route in order to refresh the page content.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您的具体问题的解决方案并未真正记录下来,似乎建议的最佳实践是仅调用您想要触发的路由中声明的函数:/
Ref:https://github.com/documentcloud/backbone/issues/1214
到目前为止,其他答案是正确的,但他们忘记提及,如果哈希导航到的是与当前哈希相同,不会触发任何内容。
The solution for your specific problem isn't really documented and it seems the suggested best practice is to just call the function that is declared in the route you want to fire :/
Ref: https://github.com/documentcloud/backbone/issues/1214
So far the other answers are correct, but they forgot to mention that if the hash navigated to is the same as the current hash, nothing will fire.
这样您就可以手动触发路线。
That way you can trigger a route manually.
假设您不仅想对“关于”页面执行此操作,请使用:
Assuming you want to do this for more than just the About page, use:
由于 Backbone 路由器会查找要加载的 URL 片段中的更改,因此对我来说有效的是首先在不触发的情况下导航到“/”,然后再导航到要使用触发器命中的 URL(片段)。如果您愿意,您可以很容易地将其构建到默认路由器行为中。
例如
Since the Backbone router looks for changes in the URL fragment you want to load, what worked for me is to navigate to '/' first without trigger, and then subsequently to the URL (fragment) you want to hit with the trigger. You could probably build this into the default router behavior pretty easily if you wanted.
e.g.
使用 Backbone 0.9.2,您传入一个选项对象并将“trigger”设置为“true”。
参考:http://documentcloud.github.com/backbone/#Router-navigate
Using Backbone 0.9.2 you pass in an options object and set 'trigger' to 'true'.
Reference: http://documentcloud.github.com/backbone/#Router-navigate
另一种选择:您可以添加一个具有随机值的参数,因此每次调用的哈希值都会不同,即使您调用同一页面。
Another option : you can add a parameter with a random value, so hash will be different for each call, even if you call the same page.
感谢@eddywashere 的建议,真的很棒。我在我的中添加了一些编辑,检查您是否位于最后一个(最新的)网址,如果是,则加载该历史记录。很简单,认为它可以帮助遇到同样问题的人。
Thanks for the suggestion @eddywashere, truly awesome. I added a little edit to mine that checks if you're on the last (most currentl) url, and if so, loads that history. Pretty simple, thought it might help someone with the same issue.