Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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