Skip to content

Instantly share code, notes, and snippets.

@kwhitaker
Created May 21, 2016 00:39
Show Gist options
  • Save kwhitaker/77bd53343241605a642af696fb5aa85f to your computer and use it in GitHub Desktop.
Save kwhitaker/77bd53343241605a642af696fb5aa85f to your computer and use it in GitHub Desktop.
module Story exposing (Model, init, view)
import Html exposing (Html, span, text)
type alias Model =
{ id : Int
, title: String
}
init : Int -> String -> Model
init id title =
{ id = id
, title = title
}
view : Model -> Html a
view model =
span []
[ text model.title ]
import Story
import Html exposing (Html, button, div, li, text, ul)
import Html.App as App
import Html.Events exposing (onClick)
main =
App.program
{ init = init
, view = view
, update = update
, subscriptions = \_ -> Sub.none
}
type alias Model =
{ stories : List Story.Model
, uid: Int
}
init : (Model, Cmd Msg)
init = ({ stories = [], uid = 1}, Cmd.none)
type Msg = Insert | Remove Int
update : Msg -> Model -> (Model, Cmd Msg)
update message ({stories, uid} as model) =
case message of
Insert ->
({ model | stories = stories ++ [ Story.init uid "foobar" ]
, uid = uid + 1
}
, Cmd.none
)
Remove id ->
({ model | stories = List.filter (\s -> s.id /= id) model.stories }
, Cmd.none
)
view : Model -> Html Msg
view model =
let
insert =
button [ onClick Insert ] [ text "New" ]
stories =
List.map viewStory model.stories
in
div []
([ul [] (stories), insert])
viewStory : Story.Model -> Html Msg
viewStory model =
li []
( [(Story.view model)
, [ button [onClick (Remove model.id) ] [ text "x" ]]
])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment