简体   繁体   English

今天(2011年)应该使用哪个JavaScript hashchange / history库?

[英]Which JavaScript hashchange/history library should I use today (2011)?

1) Which JavaScript hashchange/history library/method should I use for my JavaScript application? 1)我应该为我的JavaScript应用程序使用哪种JavaScript hashchange / history库/方法?
2) And how do I achieve these 3 things using that method? 2)如何使用该方法实现这三件事?

A) When the page loads I want to parse the url hash/parameters and set the initial application state. A)页面加载时,我想解析url哈希/参数并设置初始应用程序状态。
Specifically, my application consists of a Backbone Collection with two models which has attributes such as selectedCountry, selectedYear etc. 具体来说,我的应用程序包含一个具有两个模型的Backbone集合,这些模型具有诸如selectedCountry,selectedYear等属性。
(I don't think I can use the Backbone.Router as this is a very customized visualization app with complex states?) (我不认为我可以使用Backbone.Router,因为这是一个具有复杂状态的非常定制的可视化应用程序?)

B) I want to set up a hashchange listener or similar that lets me update the app state correspondigly B)我想设置一个hashchange侦听器或类似的东西,让我相应地更新应用程序状态

C) On Backbone Collection change events I'd like to update the url. C)在Backbone Collection上更改事件,我想更新URL。 Important: I want to remove the hashchange listener temporarily while doing this so that there is no feedback loop. 重要提示:在执行此操作时,我想暂时删除hashchange侦听器,以便没有反馈循环。

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 ... 注意:该应用程序已经依赖于某些HTML5技术,因此该解决方案不必与旧版浏览器兼容...但是“反馈回路”部分非常重要,因为我之前一直在为此苦苦挣扎...

Thanks :) 谢谢 :)

since your already using Backbone, I would stick with Backbone's Router objects. 由于您已经在使用Backbone,因此我会坚持使用Backbone的Router对象。 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 . 至于您的反馈循环问题,我已经解决了这一问题, 从来没有从code中触发我的路由器方法

The gist of it is that I let my JavaScript objects control the state of the application, and do the work for me. 要点是,我让我的JavaScript对象控制应用程序的状态,并为我完成工作。 When I do call router.navigate , I never pass true as the second argument. 当我调用router.navigate ,我从不将true作为第二个参数传递。 I only call router.navigate in response to a state change in my app, to update the hash fragment in my browser window. 我仅在响应我的应用程序中的状态更改时调用router.navigate来更新浏览器窗口中的哈希片段。 Here's the thing: This is purely a response to the state of the application having changed. 事情是这样:这纯粹是对应用程序状态已更改的响应。 I never use router.navigate to change the state of my app. 我从不使用router.navigate更改应用程序的状态。

Hope that helps 希望能有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM