Hello!
I'm new to Elm and am wondering how to update a property on a child object
of a model.
I've put together some code below to help ask the question
Thanks for your help!
import Html exposing (..)
import Html.App as Html
import Html.Events exposing (onClick)
main : Program Never
main =
Html.beginnerProgram { model = model, view = view, update = update }
type alias Model =
{ someOtherProperty : String
, child : Child
}
type alias Child =
{ currentItem : String
, items : List String
}
model : Model
model =
{ someOtherProperty = "some unrelated value"
, child = { currentItem = "Item1", items = [ "Item1", "Item2", "Item3"
] }
}
type Msg
= ChangeCurrentItem String
update : Msg -> Model -> Model
update msg model =
case msg of
ChangeCurrentItem item ->
-- { model.child | currentItem = item } -- nope
-- { model | child.currentItem = item } -- nope
{ model | someOtherProperty = item } -- << i can set
someOtherProperty on the model, how do i set the currentItem in the child?
view : Model -> Html Msg
view model =
div [] [
div [] (List.map (\item -> div [ onClick (ChangeCurrentItem item) ] [
text item ]) model.child.items)
, div [ ] [ text (model.someOtherProperty ++ " <- dont change me")]
, div [ ] [ text (model.child.currentItem ++ " <- change me
instead")]
]
--
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.