Hey folks
Absolutely - I think it would be very natural to link certain
properties in your view model with the URL history state to get "back"
button support. In fact that's pretty much exactly what I have been
doing on my own projects, more or less as Roy described.
To clarify one possible way of doing this, check out the following
working example:
http://pastie.org/1275061
This is a simple kind of master-details editor where you can navigate
through a set of products. I've used the jQuery Address plugin (http://
www.asual.com/jquery/address/), which is trivial to link to Knockout
observables. In my demo the function linkObservableToUrl(observable,
hashPropertyName) ensures that whenever the observable changes, it
goes into the URL, and whenever the URL changes, the data goes back
into the observable. This means you can use back/forwards to navigate
through the history of products you've opened.
The approach I've used on real projects is to avoid the complexity of
serializing arbitrary view model object graphs, and instead have a
specific set of string-valued observables that represent your
navigation position (in the pastie example, that's
"selectedProductName"). Based on these string values, much of the rest
of the view model state can be constructed (e.g., in the pastie
example, "selectedProduct"). This also means you can choose which
specific changes go into the URL history - e.g., in this example
editing a price doesn't go into the URL, because you probably wouldn't
want to undo that change by pressing "back" - but you would want
actions that feel like navigation to go into the URL history.
In practice this could be made more sophisticated, e.g., adding a
mechanism to update the URL asynchronously so that a sequence of
multiple state changes causes only one new URL history entry. If
there's demand for this, either I or someone else could put together a
little knockout.history.js plugin or similar.
Regards
Steve