Route update never show

  1. 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?

  1. I used above approach to handle routing between different pages, am I doing it correct? Are there better way?

Thanks.

When using routing I just store the newRoute in the Model in the init function. And, in the update function I just update the new Location in the Model. So, the model always contains an up to date location. Then in the view function I have a case-of statement to show the correct content based on the location stored in the model.

In your update function I see you are not updating the model, but using initialModel instead. Looks to me you are resetting your model to its initial state each time you change the location. Is that what you want?

my initialModel will set the route in model to current route, and view in case of current route

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.