Last active
September 9, 2017 15:12
-
-
Save thotmx/f0b0a1b5b97ccb4d8f301d482a366fb8 to your computer and use it in GitHub Desktop.
Simple Elm example for an Oaxaca.rb talk
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 (..) | |
import Html.Attributes exposing(placeholder) | |
import Html.Events exposing(onInput, onClick) | |
import Random exposing (..) | |
import Random.String | |
import Random.Char | |
type alias Model = { | |
content : String | |
} | |
-- MODEL | |
initialModel : Model | |
initialModel = { content = "Hola" } | |
-- UPDATE | |
type Msg = Update String | Reset | |
update : Msg -> Model -> (Model, Cmd Msg) | |
update msg model = | |
case msg of | |
Reset -> | |
( { model | content = "" } , generateRandomString) | |
Update newString -> | |
( { model | content = newString }, Cmd.none) | |
-- COMMANDS | |
generateRandomString : Cmd Msg | |
generateRandomString = | |
Random.generate Update (Random.String.string 5 Random.Char.english) | |
-- VIEW | |
view model = div [] [ | |
input [ placeholder "Escribe algo", onInput Update ] [] | |
, h1 [] [ text model.content ] | |
, button [onClick Reset] [ text "Reset" ] | |
] | |
-- MAIN | |
main = program { | |
init = ( initialModel, Cmd.none) | |
, update = update | |
, view = view | |
, subscriptions = ( \_ -> Sub.none ) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment