I copied this code so anyone can try it out http://elm-lang.org/try
import Html exposing (..)
import Html.App as App
import Html.Attributes exposing (..)
import Html.Events exposing (..)
main =
App.program
{ init = init
, view = view
, update = update
, subscriptions = \_ -> Sub.none
}
init =
( { posts = [], newPost = { title = "", slug = "" } }, Cmd.none )
type alias Post =
{ title : String
, slug : String
}
type alias Model =
{ posts : List Post
, newPost : Post
}
type Msg
= NoOp
| NewPostField Post String String
update msg model =
case msg of
NoOp ->
( model, Cmd.none )
NewPostField currentPost field newValue ->
case field of
"title" ->
( { model | newPost = { slug = currentPost.slug, title = newValue } }, Cmd.none )
"slug" ->
( { model | newPost = { slug = newValue, title = currentPost.title } }, Cmd.none )
-- The problem is here, I have to repeat this process for every field
_ ->
( model, Cmd.none )
view model =
div []
[ h1 [] [ text ("title : " ++ model.newPost.title ++ " | slug : " ++ model.newPost.slug) ]
, input [ onInput (NewPostField model.newPost "title"), placeholder "Title" ] []
, input [ onInput (NewPostField model.newPost "slug"), placeholder "Slug" ] []
, button [] [ text "Save" ]
]
I minimized the fields to two (title and slug), but there are others like : content, excerpt...
Is there is anyway I can update the record without creating a case for all the fields, something like updating only the field necessary without going through all of them ?