22
33This is a module for routing single-page-apps in Elm, building on the
44[ ` elm-lang/navigation ` ] ( http://package.elm-lang.org/packages/elm-lang/navigation/latest )
5- package. It is the successor to elm-route-hash:
6-
7- * now compatible with Elm 0.18, and
8-
9- * no longer limited to working only with the hash (hence the name change).
10-
5+ package.
116
127## Rationale
138
@@ -32,10 +27,12 @@ such as:
3227* [ Bogdanp/elm-route] ( http://package.elm-lang.org/packages/Bogdanp/elm-route/latest )
3328* [ etaque/elm-route-parser] ( http://package.elm-lang.org/packages/etaque/elm-route-parser/latest )
3429* [ poyang/elm-router] ( http://package.elm-lang.org/packages/poying/elm-router/latest )
30+ * [ pzingg/elm-navigation-extra] ( http://package.elm-lang.org/packages/pzingg/elm-navigation-extra/latest )
3531* [ sporto/erl] ( http://package.elm-lang.org/packages/sporto/erl/latest )
3632* [ sporto/hop] ( http://package.elm-lang.org/packages/sporto/hop/latest )
3733
38- So, what does elm-route-url do differently than the others?
34+ So, what does elm-route-url do differently than the others? First, I'll
35+ address this practically, then philosophically.
3936
4037
4138### Mapping changes in the app state to a possible location change
@@ -59,6 +56,11 @@ want to ensure is that the URL reflects the final state of your model. (For
5956instance, consider a module with an ` Increment ` message and a ` Decrement `
6057message. The URL doesn't care which way you arrived at a particular state).
6158
59+ Furthermore, every state of your model really ought to correspond with some URL.
60+ That is, given some state of your model, there must be something that you'd like
61+ to have appear in the URL. Or, to put it another way, what appears in the URL
62+ really ought to be a function of your state, not the last message you received.
63+
6264So, elm-route-url asks you to implement a function with a different signature:
6365
6466``` elm
@@ -88,28 +90,45 @@ use elm-route-url, you don't have to.
8890### Mapping location changes to messages our app can respond to
8991
9092If you use the official [ navigation] ( http://package.elm-lang.org/packages/elm-lang/navigation/latest )
91- package in Elm 0.18 directly, the ` Navgation.program ` differs from the
92- standard ` Html.program ` in two ways:
93-
94- First, you are asked to implement an argument
95- to ` Navigation.program ` that converts a ` Location ` to a message
96- whenever the URL changes.
97-
98- Second, the ` Navigation.program ` takes an init function that
99- takes a ` Location ` as an argument. This lets you use the URL on the first frame.
93+ package in Elm 0.18 directly, you react to location changes by providing
94+ an argument to ` Navigation.program ` which converts a ` Location ` to a message
95+ your app can deal with. Those messages are then fed into your ` update ` function
96+ as the ` Location ` changes.
10097
101- In elm-route-url, this functionality for both of these is handled by asking
102- you to implement a function with a different signature:
98+ On the surface, elm-route-url works in a similar manner, except that it
99+ asks you to implement a function which returns a list of messages.
100+ (This is possibly a convenience when you need multiple messages to
101+ react to the URL change, though of course you could also redesign your
102+ app to do multiple things with a single message).
103103
104104``` elm
105105location2messages : Location -> List Message
106106```
107107
108- ` location2messages ` will be called when the underlying ` Navigation.program `
109- ` init ` method is invoked, so you don't need to change that in your
110- program's code. And of course ` location2messages ` will also be called every
111- time the location is changed externally (not from a state change that
112- generated a new location via ` delta2url ` ).
108+ ` location2messages ` will also be called when your ` init ` function is invoked,
109+ so you will also get access to the very first ` Location ` .
110+
111+ So, that is similar to how ` Navigation ` works. The difference is that
112+ ` Navigation ` will send you a message even when you programmatically change
113+ the URL. By contrast, elm-route-url only sends you messsages for ** external**
114+ changes to the URL -- for instance, the user clicking on a link, opening
115+ a bookmark, or typing in the address bar. You won't get a message when you've
116+ made a change in the URL due to your ` delta2url ` function, since your state
117+ is already in sync with that URL -- no message is required.
118+
119+
120+ ### Philosphically
121+
122+ You can, if you are so inclined, think about those differences in a more
123+ philosophical way. There is a [ thread] ( https://groups.google.com/forum/#!topic/elm-discuss/KacB1VqkVJg/discussion )
124+ on the Elm mailing list where Erik Lott gives an excellent summary.
125+ The question, he says, is whether the address bar should drive the model,
126+ or whether the model should drive the address bar. For more details,
127+ read the thread -- it really is a very good summary.
128+
129+ Another nice discussion of the philosophy behind elm-route-url is in a blog post
130+ by Amitai Burstein, under the heading
131+ [ URL Change is not Routing] ( http://www.gizra.com/content/thinking-choosing-elm/#url-change-is-not-routing )
113132
114133
115134## API
@@ -127,6 +146,8 @@ moment. How you parse the `Location` (and construct a `UrlChange`) is pretty
127146much up to you. Now, I have included a ` RouteUrl.Builder ` module that could
128147help with those tasks. However, you don't need to use it -- many other
129148approaches would be possible, and there are links to helpful packages above.
149+ For my own part, I've been using [ evancz/url-parser] ( http://package.elm-lang.org/packages/evancz/url-parser/latest )
150+ recently to implement ` location2messages ` .
130151
131152The ` RouteHash ` module attempts to match the old API of elm-route-hash as
132153closely as possible. You should be able to re-use your old ` delta2update ` and
0 commit comments