-
-
Save lukewestby/3dc48266b15a4837a79ddd71e58c6999 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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