I have an update function that has to call update functions for each
registered page. The update function of the page itself only returns the
page model and the command, whereas the main update returns it's own model
(all pages) and a command. Here is the painful code:
type alias Model =
{ home : Home.Model
, about : About.Model
}
type Msg
= HomeMsg Home.Msg
| AboutMsg About.Msg
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
HomeMsg msg_ ->
let
( page, cmd ) =
Home.update msg_ model.home
in
( { model | home = page }, Cmd.map HomeMsg cmd )
The code in the case block is long and painful when you have X case values.
So i tried to simplify this code with a function that maps the return value
of the page update to the return value of the main update:
lift : (msg -> Msg) -> (Model -> a -> Model) (a, Cmd msg) -> (Model, Cmd
Msg)
lift cmdMapper modelMapper (m, c) =
( (modelMapper m), (Cmd.map cmdMapper c) )
setHome : Model -> a -> Model
setHome model page =
{ model | home = page }
And in the main update case I have this line:
lift HomeMsg (setHome model) <| Home.update msg_ model.home context
This is a lot simpler but still I have to declare a function (like setHome)
for each case value to update my model page field.
A better way to update model field more "dynamically" would have been great
to only need 1 function and not one for each case value
--
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.