Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
52 lines (48 sloc) 1.74 KB
(ns papadom.example.todo
"Simple todo example."
[cljs.core.async :refer [chan >! <!]]
[papadom.template :refer [compile-templates input-seq->map render
template-events value->select-options]])
[cljs.core.async.macros :refer [go]]))
(defn show-state
"Takes the application state, and renders it on the page using the templates."
(render "state"
(assoc state
:total (count (:todos state))
:remaining (count (filter :done (:todos state))))))
(defn app
"Reads events and modifies the state."
[state event-chan]
(loop [[event event-data] (<! event-chan)]
(.log js/console (str "event: " event " event-data: " event-data))
(case event
(swap! state update-in [:todos]
conj {:text (:text (input-seq->map event-data))
:done false})
(let [v (get-in state [:todos (:index event-data) :done])
nv (boolean (:checked event-data))]
(when (not= v nv)
(swap! state assoc-in [:todos (:index event-data) :done] nv)))
(swap! state update-in [:todos] #(vec (remove :done %))))
(recur (<! event-chan)))))
(defn start
"Read the templates, set up the events channel, and start the app with
some initial state."
(let [event-chan (chan)
state (atom nil)]
(template-events event-chan)
(add-watch state :state (fn [key ref old new] (show-state new)))
(reset! state initial-state)
(app state event-chan)))
([] (start
{:todos [{:text "Learn papadom" :done false}
{:text "Build a papadom app" :done false}]})))