In terms of sending an HTTP request when navigating to the Login page, you 
should be able to do that by returning an Http Cmd in your `update` 
function.

Elm's Navigation library provides a special `program` function which--in 
addition to the standard init/update/view/subscription functions used by 
Html.program--also  takes a function from `Navigation.Location` to a 
message that your update function can understand. In the example program 
included in the Navigation library, this function is called `UrlChange`.

So in your `update` function, you probably already have a case that listens 
for this `UrlChange` message, computes a "route" from the `Location` 
attached to this message, and stores the new route in your model. 

update msg model =
  case msg of
    UrlChange location ->
      ( { model | route = routeFromLocation location }
      , Cmd.none
      )


All you need to do is change this case so that in addition to returning the 
updated model, it looks at the new route and returns a Cmd based on whether 
the new route is Login or not. 

e.g.

update msg model =
  case msg of
    UrlChange location ->
       let
           newRoute = 
                routeFromLocation location

           newCmd =
                case newRoute of
                      Just (Route.Login) ->
                              Http.get "/api/loginStuff" stuffDecoder 
                                  |> Http.send GotStuff
                      
                      _ ->
                          Cmd.none
       in
           ( { model | route = newRoute }
           , newCmd 
           )


On Sunday, February 5, 2017 at 11:51:03 AM UTC-8, Kingsley Hendrickse wrote:
>
> Hi, 
>
> I'm new to elm and am using version 0.18
>
> I'm struggling a bit to get what I want out of the Route and Navigation 
> libraries.
>
> I have 2 features Login and Registration 
>
> In the Main.elm I use an update which delegates to either the Login or 
> Registration features and then updates the main model after Login or 
> Registration has done its thing.
>
> However I want to be able to detect when I'm going to show the Login view 
> and before the view is rendered fire off an http request which will be used 
> to populate the view. I can't see how to do this. It's easy to do as a 
> result of something like a button click - because then the trigger point is 
> the button click. But I want the trigger point to be during navigation so 
> that the user can arrive on a page and that initial arrival on the page 
> triggers the http requests that will be used in that view. 
>
> Also if anyone could point me in the direction of a good pattern for 
> protecting routes that would be great - e.g. if a user is not logged in 
> they should not be allowed to go to the welcome page and get redirected to 
> login, and if logged in they should be redirected to welcome and not 
> allowed on login or registration etc ...
>
> Any help appreciated
>
> Thanks
>
> --Kingsley
>

-- 
You received this message because you are subscribed to the Google Groups "Elm 
Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to