A follow up to the Navigation episode, evancz/url-parser helps you parse a Navigation.Location
record into a union type. Including that type in your model allows you to update the view based on the page the user requests.
Examples
$ elm-package install evancz/url-parser
Routes.elm
type Page
= Home
| About
| Theme String
pageParser : Parser (Page -> a) a
pageParser =
oneOf
[ map Home (s "")
, map About (s "about")
, map Theme (s "theme" </> string)
]
pathParser : Navigation.Location -> Maybe Page
pathParser location =
UrlParser.parsePath pageParser location
Main.elm
modelWithLocation : Location -> Model -> Model
modelWithLocation location model =
let
page =
location
|> Routes.pathParser
|> Maybe.withDefault Home
in
{ model | page = page }
initialState : Location -> ( Model, Cmd Msg )
initialState location =
modelWithLocation location initialModel ! []
update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
case msg of
UrlChange location ->
modelWithLocation location model ! []
Links