-
By some help here, I finally get a 2 pages with routing working, my init and update code is below:
init : Location -> ( Model, Cmd Msg ) init location = let newRoute = Route.extractRoute location in case (Debug.log "initRoute" newRoute) of IndexRoute -> ( initialModel newRoute, fetchIndexCommand ) CategoryRoute categoryId -> ( initialModel newRoute, fetchCategoryInfoCommand categoryId ) NotFoundRoute -> ( initialModel newRoute, Cmd.none ) update : Msg -> Model -> ( Model, Cmd Msg ) update msg model = case (Debug.log "msg" msg) of LocationChanged location -> let newRoute = Route.extractRoute location in case (Debug.log "updateRoute" newRoute) of IndexRoute -> ( initialModel newRoute, fetchIndexCommand ) CategoryRoute categoryId -> ( initialModel newRoute, fetchCategoryInfoCommand categoryId ) NotFoundRoute -> ( initialModel newRoute, Cmd.none ) ...
What I do not understand is, whenever I enter the 2 location, or click the links on 2 pages to jump around, the console always show “initRoute” only, never show “updateRoute”. The LocationChanged location msg will always be generated right? Why didn’t it show up?
- I used above approach to handle routing between different pages, am I doing it correct? Are there better way?
Thanks.