今天(2011 年)我应该使用哪个 JavaScript hashchange/history 库?
1) 我应该为我的 JavaScript 应用程序使用哪种 JavaScript hashchange/历史库/方法?
2)我如何使用该方法实现这三件事?
A) 当页面加载时,我想解析 url 哈希/参数并设置初始应用程序状态。
具体来说,我的应用程序由一个带有两个模型的 Backbone Collection 组成,这些模型具有 selectedCountry、selectedYear 等属性。
(我不认为我可以使用 Backbone.Router,因为这是一个非常定制的具有复杂状态的可视化应用程序?)
B)我想设置一个 hashchange 监听器或类似的,让我相应地更新应用程序状态< /strong>
C) 在 Backbone Collection 更改事件中,我想更新 url。重要提示:我想在执行此操作时暂时删除 hashchange 侦听器,以便不存在反馈循环。
注意: 该应用程序已经依赖于一些 HTML5 技术,因此该解决方案不必与旧版浏览器兼容...但是“反馈循环”部分很重要,因为我之前一直在努力解决这个问题...
谢谢:)
1) Which JavaScript hashchange/history library/method should I use for my JavaScript application?
2) And how do I achieve these 3 things using that method?
A) When the page loads I want to parse the url hash/parameters and set the initial application state.
Specifically, my application consists of a Backbone Collection with two models which has attributes such as selectedCountry, selectedYear etc.
(I don't think I can use the Backbone.Router as this is a very customized visualization app with complex states?)
B) I want to set up a hashchange listener or similar that lets me update the app state correspondigly
C) On Backbone Collection change events I'd like to update the url. Important: I want to remove the hashchange listener temporarily while doing this so that there is no feedback loop.
Note:
The app is already relying on some HTML5 technologies so the solution does not have to be compatible with the older browsers ... But the "feedback loop" part is important as I've struggled with this before ...
Thanks :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因为您已经在使用 Backbone,所以我会坚持使用 Backbone 的
Router
对象。使用应用程序中已有的内容会比尝试添加新内容更容易。至于您的反馈循环问题,我已经通过 永远不要从代码中触发我的路由器方法。
其要点是,我让 JavaScript 对象控制应用程序的状态,并为我完成工作。当我调用 router.navigate 时,我从不传递 true 作为第二个参数。我仅调用
router.navigate
来响应应用程序中的状态更改,以更新浏览器窗口中的哈希片段。事情是这样的:这纯粹是对应用程序状态已更改的响应。我从不使用 router.navigate 来更改应用程序的状态。希望有帮助
since your already using Backbone, I would stick with Backbone's
Router
objects. It will be easier to use what's already available in your app instead of trying to bring something new in the mix.As for your feedback loop problem, I've solved this by never firing my router methods from code.
The gist of it is that I let my JavaScript objects control the state of the application, and do the work for me. When I do call
router.navigate
, I never passtrue
as the second argument. I only callrouter.navigate
in response to a state change in my app, to update the hash fragment in my browser window. Here's the thing: This is purely a response to the state of the application having changed. I never userouter.navigate
to change the state of my app.Hope that helps