Last active
August 22, 2016 16:32
-
-
Save kittykatattack/f2717f5b8111d7dd3bc5bbfdc4ccf309 to your computer and use it in GitHub Desktop.
Flat Elm UI Component Architecture Experiment
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, button, div, text) | |
import Html.App as Html | |
import Html.Events exposing (onClick) | |
main = | |
Html.beginnerProgram | |
{ model = model | |
, view = view | |
, update = update | |
} | |
-- MODEL | |
type alias Model = | |
{ buttonOne : ButtonModel | |
, buttonTwo : ButtonModel | |
} | |
model : Model | |
model = | |
{ buttonOne = buttonModel | |
, buttonTwo = buttonModel | |
} | |
-- The button component | |
type alias ButtonModel = Int | |
buttonModel : ButtonModel | |
buttonModel = | |
0 | |
type Msg | |
= Increment | |
| Decrement | |
buttonUpdate : Msg -> ButtonModel -> ButtonModel | |
buttonUpdate msg model = | |
case msg of | |
Increment -> | |
model + 1 | |
Decrement -> | |
model - 1 | |
buttonView : ButtonModel -> Html Msg | |
buttonView model = | |
div [] | |
[ button [ onClick Decrement ] [ text "-" ] | |
, div [] [ text (toString model) ] | |
, button [ onClick Increment ] [ text "+" ] | |
] | |
-- UPDATE | |
update : Msg -> Model -> Model | |
update msg model = | |
--{ model | buttons = List.map buttonUpdate (msg model.buttons) } | |
{ model | |
| buttonOne = buttonUpdate msg model.buttonOne | |
, buttonTwo = buttonUpdate msg model.buttonTwo | |
} | |
-- VIEW | |
view : Model -> Html Msg | |
view model = | |
div [] | |
[ buttonView model.buttonOne | |
, buttonView model.buttonTwo | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Working version