Created
April 12, 2016 21:31
-
-
Save chuck0523/4a8a5fd5a0def4885cb2f998334bcf44 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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