Skip to content

Instantly share code, notes, and snippets.

@lukewestby
Last active May 2, 2016 12:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lukewestby/3dc48266b15a4837a79ddd71e58c6999 to your computer and use it in GitHub Desktop.
Save lukewestby/3dc48266b15a4837a79ddd71e58c6999 to your computer and use it in GitHub Desktop.
import Html exposing (Html, div, text)
import Html.App
import Html.Events exposing (onClick)
type alias Model =
{ ones : List Int }
model : Model
model =
{ ones = [] }
type Msg
= One
update : Msg -> Model -> Model
update msg model =
case msg of
One ->
{ model | ones = 1 :: model.ones }
view : Model -> Html Msg
view model =
div [] [ viewOnes model.ones ]
-- if you click any of these divs, a Msg of One will be fired because
-- the click handler in viewAddOne is also attached to all of its
-- siblings
viewOnes : List Int -> Html Msg
viewOnes ones =
let
onesViews =
List.map viewOne ones
children =
List.append onesViews [ viewAddOne ]
in
div [] children
viewOne : Int -> Html Msg
viewOne one =
div
[]
[ div [] [ text "One" ]
, div [] [ text (toString one) ]
]
viewAddOne : Html Msg
viewAddOne =
div
[ onClick One ]
[ text "Add One" ]
main : Program Never
main =
Html.App.beginnerProgram
{ view = view
, update = update
, model = model
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment