Skip to content

Instantly share code, notes, and snippets.

@chuck0523
Created April 12, 2016 21:31
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 chuck0523/4a8a5fd5a0def4885cb2f998334bcf44 to your computer and use it in GitHub Desktop.
Save chuck0523/4a8a5fd5a0def4885cb2f998334bcf44 to your computer and use it in GitHub Desktop.
module TodoList where
import Todo
import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (..)
-- model
type alias Model =
{ todos : List (ID, Todo.Model)
, nextID : ID
}
type alias ID = Int
init : Model
init =
{ todos = []
, nextID = 0
}
-- action
type Action = Add
update : Action -> Model -> Model
update action model =
case action of
Add ->
let newTodo = ( model.nextID, Todo.init "do something" )
newTodos = model.todos ++ [ newTodo ]
in
{ model |
todos = newTodos,
nextID = model.nextID + 1
}
-- view
view : Signal.Address Action -> Model -> Html
view address model =
let
todos = List.map (viewTodos address) model.todos
add = button [onClick address Add ] [ text "+" ]
in
div [ todosStyle ] ([add] ++ todos)
viewTodos : Signal.Address Action -> (ID, Todo.Model) -> Html
viewTodos address (id, model) =
Todo.view model
todosStyle : Attribute
todosStyle =
style
[ ("margin", "10px")
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment