I've read a dozen of times now that the state object could exists of multiple key|value pairs and that it is associated with the new history entry. But could someone please give me an example of the benefits of the state object? Whats the practical use of it? I can't imagine why not just typing in {}
相关问题
- Do the Java Integer and Double objects have unnece
- Backbone.js PushState routes .htaccess only workin
- How do I create a multidimensional array of object
- Union of two object bags in Java
- Should this property be part of my object's in
相关文章
- HTML5 control
- Poor Use Case of Object.assign() - Simple Example
- Why is this factory returning a $$state object ins
- Where is the object browser in visual studio C# 20
- Implementation Strategies for Object Orientation
- An object reference is required for the non-static
- How to use object spread with nested properties?
- Python object containing an array of objects being
Take this small example: run fiddle (editor view):
You have a page where a user can select a color. Every time they do, we generate a new history entry:
We leave the state object blank for now and set the URL to the color name (don't reload the page - that URL doesn't exist, so you will get a 404).
Now click on a red, green and blue once each. Note that the URL changes. Now what happens if you click the back button?
The browser does indeed go back in history, but our page doesn't notice that - the URL changes from '/blue' back to '/green', but our page stays at 'You have selected blue'. Our page has gone out of sync with the URL.
This is what the
window.onpopstate
event and the state object are for:we include our selected color in our state object
Then we listen for the
popstate
event, so that we know when we have to update the selected color, which (in jQuery) is this:Try the updated example: run fiddle (editor view): our page now updates accordingly when the user navigates back through history.
The most obvious example I can see for
pushState
,replaceState
, andwindow.onpopstate
is Ajax navigation for a site.Say your site or web app has fixed
nav
&footer
.. That way you can load content of new pages into a specific container, say the new<main>
element.You probably won't want to be reloading the page, and just load what you need from the new page.
Using the
pushState
method means you'll be able to use your browser's Back and Forward buttons to navigate, even though you're not reloading the page.Nifty, eh?
From MDN:
Example
There's a great example on MDN here, and the relevant Git repo here.
Is a specific and forward looking use case the maintenance of user view and data state in a progressive app using custom elements and templates that are divided up in the view regionally
Imagine a 64 box grid as your view, on a large screen the boxes are 147 ^2 a piece
The url represents 64/ a user ID abs related user data
The web app can then fill its grid with user specific state data
In this use case, one I fully believe is the future, the user wouldn't want to share his or her personal state and data filled view portions
By using previous history states and their related 650k of data
A whole, complex app can be, reassembled from browser history and location, including state, using a few well known sort approaches.
It's cool