OK, I just don’t quite understand, even while I have working code!
I have an app where, as the model changes (in particular the value representing the ‘page’ of my single page app), the Url is updated with this task
Navigation.newUrl (toUrl model)
But as the Url changes, I get a new message from Navigation because of:
main =
Navigation.programWithFlags
(Routing.urlParser RouteTo)
{ init = initWithFlags
, update = Routing.update
, view = view
, subscriptions = subscriptions
}
When first loading the app, this message is useful and I can use it to direct to the appropriate opening page. But thereafter this message is redundant - I already made all the model changes I wanted before and that caused the Url update. Without some care I even get a loop of each Routing message causing the
I suspect I am missing something rather important, but am not sure what?
One option would be to use anchor tags to cause the switch pages, and only do model changes when I get a RouteTo
message, but even then I have some url changes resulting from the clicking on an element within a page, and I don’t think that is a place for anchors. (As I use html urls, rather than #
ones I also have to be careful not to let the page get reloaded)
One of the main benefits of routing/navigation is that a user can bookmark or send a URL to someone else, and the website loads up in the correct state. Option number 1 seems like it would handle this case naturally. I'm not sure how it would work for option number 2.
Is there any reason to favour one over the other, otherwise I'm likely to go with 2.
Here is some code to make things more concrete
https://gist.github.com/simonh1000/9368f9dbd7f93646207ec27fdf3662a2
It is based on the example from the Navigation library, but with the links changed from #
to (I think they are called) HTML5 links.
I added an onClick
handler to provide a preventDefault
as otherwise the links 404, but with this handler the links don’t navigate instead
The aim is to get the navigation history ticking along properly without page refreshes.
I know its possible as I’ve seen it in other routers
Simon
With option 1 how do you stop the page reloading on each click on an anchor?
Another Con of option 1 might be that it can't cover so many scenarios. Suppose that you have a form, click submit and then - on successful completion of some persistence stage - you want to redirect to another page. (Or is that possible by combining some kind of redirect command with the confirmation from the server, and again here how would you prevent page reloading?)
The aim is to get the navigation history ticking along properly without page refreshes.
Basically you have two ways to move around the app (not to use the word ‘navigate’):
on "click" {preventDefault = True, ...} ...
to prevent the page being totally reloaded.
Whenever the url is changed, Navigation will create a new message that you can either ignore (if you are updating your model in the original event handler), or you wait till the Navigation message hits and take action at that stage. Notionally the latter is more elegant, but I don’t think it makes much practical difference.