Skip to content

Instantly share code, notes, and snippets.

@piotrkubisa
Last active June 20, 2016 20:07
Show Gist options
  • Save piotrkubisa/c91e6b149f3d01e3a19ded19d2597037 to your computer and use it in GitHub Desktop.
Save piotrkubisa/c91e6b149f3d01e3a19ded19d2597037 to your computer and use it in GitHub Desktop.
import Html exposing (Html, Attribute, input, strong, ul, li, em, div, text, button)
import Html.App as Html
import Html.Attributes exposing (..)
import Html.Events exposing (onInput, onClick)
main =
Html.beginnerProgram { model = model, view = view, update = update }
-- MODEL
type alias Model =
{ subject : String
, previous: List String
}
model : Model
model =
{ subject = ""
, previous = ["World", "Earth"]
}
-- UPDATE
-- Delegates
type Message
= ReplaceSubject String
| AddItem
-- Something like Message<Model in, Model out>
update : Message -> Model -> Model
update message model =
case message of
ReplaceSubject newMessage ->
{ model | subject = newMessage }
AddItem ->
{ model | previous = model.subject :: model.previous}
-- VIEW
prevEls previous =
let
children = List.map prevItem previous
in
ul [] children
prevItem item =
li [] [ text item ]
view : Model -> Html Message
view model =
div [] [
div []
[ input [placeholder "Placeholder", onInput ReplaceSubject] [text model.subject]
, button [onClick AddItem] [text "Add"]
]
, div []
[ prevEls model.previous
]
, div []
[ strong [] [text "Hello"]
, em [] [text model.subject ]
]
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment