New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make Backbone.history.historyStarted accessible to developers. #538
Conversation
This has been discussed in IRC and while I think this should be pulled, there are conflicting opinions. I'll mark this for needs review. I think opinions should be weighed in. My personal opinion is that there should be some mechanism for finer control over Backbone history and perhaps a documented interface into the internals may be better in case historyStarted changes in the future. |
I would agree there should be some mechanism to determine if history has been started, perhaps an option to the start method would be more appropriate. In my case different Backbone routers can be used on the page depending on which dialog box is opened. When these close one dialog box, what I really like to have is Backbone.history.stop() which would then allow the next dialog box the user might interact with to call Backbone.history.start() naively. As it stands, I believe I'm left with the following option
|
This private state makes it a little ugly to isolation test backbone applications (e.g. with Jasmine), since I have to wrap
I would also be happy |
+1. I too am having to wrap this in a try/catch for running in a test environment, which makes code uglies. |
+1 - history.stop is needed for unit-testing |
+1 |
Thanks for working out the proper approach -- exposing |
A situation where historyStarted is needed and stop() will not do: Using TypeScript to "simulate" inheritance of a BaseRouter class ("simulate" because after all it has a pure Javascript manifestation) that calls start() in its constructor. Now inheriting from this class more than once becomes a problem because the second derived class (or a second instance of the same derived class) will throw an error when it calls its base constructor (upon instantiation). I have precisely this problem now and am forced to use this monstrosity in the base constructor:
|
@parliament718 Try this: if (!Backbone.History.started) Backbone.history.start({ pushstate: true} ); |
@parliament718 That seems like a flaw in TypeScript, it shouldn't be automatically calling up |
I just noticed this has the very peculiar effect of adding a "#" to each of my routes. As in after I change from my ugly try/catch blocks to check Backbone.History.started: if (!(Backbone.History).started) Backbone.history.start({ pushstate: true} ); All of my routes end up as "site.com/#Controller" rather than "site.com/Controller". I have toggled between the 2 lines several times and each time correctly confirmed it's exactly that switch that causes this. I'm switching back to the try/catch for now until this can be resolved. Thanks |
That is peculiar. Can you make a jsfiddle or similar showing just this bug |
Just a minor edit.
In my example I call Backbone.history.start really late because of Facebook authentication reasons and when a user's session is disconnected I must call an .init script again where I include a check to see if we're already listening for hash changes.