First of all, you're right: "init" is a function like everything else in
Elm. Even "Model" is a function (a constructor) if you defined it as "type
alias". However, no function is triggered automatically in Elm, unless you
trigger them. Init is typically called in the "main" function but it can be
called elsewhere, too, if necessary, where its output (Model, Cmd Msg)
fits. An example is the "update" function like this:
update action model =
case action of
Logout ->
init
Back to your problem. Check whether you model gets updated after ActionA.
(Use monitor in Elm-Reactor) Is it? Perhaps only the View is not?
As far as I can see the issue is with your view function. You must have a
"root" view and handle the view element conditional changes with separate
subview-functions. My experience is that if you put the condition right to
the "root" view, the view does not get updated with each change of the
Model.
Hope this helps.
On Monday, April 10, 2017 at 5:48:39 PM UTC+2, Oliver Dunkl wrote:
>
> When will the init function call?
>
> I have a strange (for me) behaviour in my flow. I have a backend call
> in which some entity will be saved. I have a page redirect logic e.g.:
>
> type alias Model
> { page : Int
> }
>
> view model =
> case model.page of
> 0 -> viewA
> 1 -> viewB
> ...
>
> In my 'update' function I will set this pages:
>
> init : (Model, Cmd Action)
> init =
> let model = Model 0
> in (model, Cmd.batch [])
>
> update action model =
> case action of
> ActionA -> model ! [ saveEntity ]
> SaveE (Err _) -> model ! []
> SaveE (Ok _) -> { model | page = 1 } ! []
>
> I have a button which triggers ActionA and this sends a backend-call. So
> sometimes it works as expected and sometimes the whole page will be
> reloaded after SaveE (Ok _) and the init function will be called and my
> model is
> initialized with the starting values.
>
> The backend gets some values and saves the entity in the db. This
> works fine with a curl-call.
>
> So my question is, is there a trigger which reloads the page and calls
> the init function? And has someone an idea why the init function will
> called in the
> middle of a flow?
>
> My problem is it seems that this happens randomly and I have currently
> no idea where the problem comes from.
>
> thx
> \= odi
>
--
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.