How do you handle situations like this:
Your application navigates to some route that involves loading some data. Lets say its a folder that may contain some images.
Browser.Navigation.pushUrl key "/myapp/folder/1/"
results in
Browser.application.onUrlChange
being called. The URL is parsed to get a new route, and this triggers the next step, which is to see what is in the folder.
An HTTP request is made to fetch the contents of folder 1. There may be no images in it, but in this case a list of image files and their URLs comes back. In this particular application, if there are no image files we just want to list what files are in the folder (maybe some non-image files), but if there are images, we want to show the first one. The Model
is updated to reflect that a single image is to be shown, and the view
displays it. Since we are now on a particular image, we want to update the URL to reflect that (in case the user bookmarks it):
Browser.Navigation.pushUrl key "/myapp/folder/1/holliday-snaps.jpg"
results in
Browser.application.onUrlChange
being called. The URL is parsed to get a new route, and this is different to the folder route we were on previously. This application reacts to this route change by loading the image again, even though that is the state we are already in.
In other words, I just want to change the URL, and not fire an event that will trigger whatever side-effects the application generates when switching to a new route.
Seems like one answer is that I should store the current route in the top-level Model
, change that to ViewImage (Folder 1) "holliday-snaps"
, then push that URL. In onUrlChange
compare the new route to the current one, find that there is no change, and fire a Noop
to ignore it.
Sometimes feels like there should be additional options to Browser.Navigation.pushUrl/replaceUrl
that simply sets the URL in the browser, but does not trigger a navigation event to Browser.application.onUrlChange
.
I feel this area of Elm applications can easily get a bit muddled, and interested to hear how others deal with this to ensure that the URL is always accurate and changes to it do not result in duplication of side-effects.