backbone and history push state
-
07-06-2021 - |
Question
Using backbone, is it any way to store some data in the history so it can be retrived when a back is called?
In a none backbone application I the application will be something like the following. When executing an action:
//When doing some action
history.pushState(mycurrentData, title, href)
and the following to retvive the current Data in case of back:
function popState(event) {
if (event.state) {
state = event.state;
//get my data from state
}
}
window.onpopstate = popState;
I need to apply the same behaviour on my backbone app.
Thanks
Solution
At this point, it's not possible with Backbone directly
http://backbonejs.org/docs/backbone.html#section-139
You can see a couple of lines down at that point in the code:
window.history[options.replace ? 'replaceState' : 'pushState']({}, document.title, frag);
So, it's setting the data to an empty object, all the time.
To make this work, then, you'd have to store the data on your own and use some other method of retrieving the data when your route fires.
Personally, I think it would be worth patching Backbone to allow data storage in the history. But that's a thought for the Backbone issues list :)
EDIT
It looks like someone else wanted to do this and it was shot down: https://github.com/documentcloud/backbone/pull/660