Get updates via , Github, or RSS | About

Episode 30: Update.Extra Mar 31, 2017

Because model is such a common name to use in your update function, it can be tricky to ensure you’re always using the most updated version. Whether it is model_, model2, or updatedModel, subtle bugs can work their way into your code if you use the wrong one.

You can almost completely eliminate that problem if you use the pipe operator |> and never bind anything. The Update.Extra module makes that easy.

Examples

Main.elm

update : MsgDetail -> Model -> ( Model, Cmd Msg )
update msg model =
    case msg of
        Edit listId id name ->
            Lists.update (Create listId) model
                |> Tuple.mapFirst (updateDetail id name)
                |> Update.addCmd (writeToLocalStorage name)
                |> andThen Server.update Save


andThen : (msg -> model -> ( model, Cmd a )) -> msg -> ( model, Cmd a ) -> ( model, Cmd a )
andThen update msg ( model, cmd ) =
    let
        ( model_, cmd_ ) =
            update msg model
    in
        ( model_, Cmd.batch [ cmd, cmd_ ] )