Skip to content

Instantly share code, notes, and snippets.

@naltatis
Created January 26, 2016 20:58
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 naltatis/a3df9f8a563bfe752f04 to your computer and use it in GitHub Desktop.
Save naltatis/a3df9f8a563bfe752f04 to your computer and use it in GitHub Desktop.
elm-samples
module Counter (..) where
import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (..)
import Signal exposing (Address)
import StartApp.Simple as StartApp
-- MODEL
type alias Model =
{ count : Int }
initialModel : Model
initialModel =
{ count = 0 }
-- UPDATE
type Action
= Add Int
| Reset
update : Action -> Model -> Model
update action model =
case action of
Add amount ->
{ model | count = model.count + amount }
Reset ->
{ model | count = 0 }
-- VIEW
view : Address Action -> Model -> Html
view address model =
div []
[ h1 [ class "headline" ] [ text "Zähler" ]
, button [ onClick address (Add 1) ] [ text "+ 1" ]
, button [ onClick address (Add 2) ] [ text "+ 2" ]
, button [ onClick address Reset ] [ text "zurücksetzen" ]
, hr [] []
, strong [] [ text ("Summe: " ++ (toString model.count)) ]
]
-- WIRE IT ALL TOGETHER
main : Signal Html
main =
StartApp.start
{ model = initialModel
, view = view
, update = update
}
module CounterTrail (..) where
import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (..)
import Signal exposing (Address)
import StartApp.Simple as StartApp
-- MODEL
type alias Model =
{ trail : List Int }
initialModel : Model
initialModel =
{ trail = [] }
-- UPDATE
type Action
= Add Int
| Reset
update : Action -> Model -> Model
update action model =
case action of
Add amount ->
{ model | trail = amount :: model.trail }
Reset ->
{ model | trail = [] }
-- VIEW
item : Int -> Html
item amount =
li [] [ text (toString amount) ]
view : Address Action -> Model -> Html
view address model =
div []
[ h1 [ class "headline" ] [ text "Zähler" ]
, button [ onClick address (Add 1) ] [ text "+ 1" ]
, button [ onClick address (Add -1) ] [ text "- 1" ]
, button [ onClick address (Add 10) ] [ text "+ 10" ]
, button [ onClick address (Add -10) ] [ text "- 10" ]
, button [ onClick address Reset ] [ text "zurücksetzen" ]
, hr [] []
, strong [] [ text ("Summe: " ++ (toString (List.sum model.trail))) ]
, ul [] (List.map item model.trail)
]
-- WIRE IT ALL TOGETHER
main : Signal Html
main =
StartApp.start
{ model = initialModel
, view = view
, update = update
}
{
"version": "1.0.0",
"summary": "helpful summary of your project, less than 80 characters",
"repository": "https://github.com/user/project.git",
"license": "BSD3",
"source-directories": [
"."
],
"exposed-modules": [],
"dependencies": {
"elm-lang/core": "3.0.0 <= v < 4.0.0",
"evancz/elm-html": "4.0.2 <= v < 5.0.0",
"evancz/start-app": "2.0.2 <= v < 3.0.0"
},
"elm-version": "0.16.0 <= v < 0.17.0"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment