todomvc closer

This commit is contained in:
mike-thompson-day8 2015-03-03 01:03:23 +11:00
parent 95de45a252
commit a00f9f67ad
1 changed files with 27 additions and 31 deletions

View File

@ -8,17 +8,15 @@
path trim-v debug]]))
;; TODOs
;; add .gitignore
;; Get preoject.cljs up to speed `lein run` lein debug`
;; split into files view, handlers, subs, middleware
;; load todos off localstorage via merge ... and write back
;; Show off debugging capabiliteis
;; Add Prismatic schema - modules called state
;; change example.html to todo.html
;; add middleware to save to local storage - beware using
;; check that (path ) does an identity test before swaping back in
;; add middleware to save to local storage
;; -- Helpers -------------
(enable-console-print!)
(defn next-id
@ -37,8 +35,9 @@
(defn completed-count
"return the count of todos which have a :done of true"
[todos]
(count (filter :done todos)))
(count (filter :done (vals todos))))
;; -- Middleware -------------
;;
@ -63,15 +62,14 @@
;;
;; Middleware means our handlers are pure and simple.
;;
(def todo-middleware (comp (path [:todos])
debug
trim-v ))
(def todo-middleware [(path [:todos]) debug trim-v])
;; -- Handlers -------------
;; we dispatch this event on program startup - responsible for initialising-db
(register-pure-handler
:initialise-db
[debug] ;; middleware
(fn
[_ _]
{:todos (sorted-map)
@ -79,7 +77,7 @@
(register-pure-handler
:set-showing
(comp debug trim-v)
[debug trim-v] ;; middleware
(fn
[db [filter-kw]]
(assoc db :showing filter-kw)))
@ -93,13 +91,16 @@
(assoc todos id {:id id :title text :done false}))))
(register-pure-handler
:complete-all
:complete-all-toggle
todo-middleware
(fn
[todos [val]]
[todos []]
(let [val (every? (comp true? :done) (vals todos))]
(println "todos " todos)
(println "val " val)
(reduce #(assoc-in %1 [%2 :done] val)
todos
(keys todos))))
(keys todos)))))
(register-pure-handler
:toggle-done
@ -128,13 +129,12 @@
(fn
[todos _]
(->> (vals todos)
(remove :done)
(filter :done)
(map :id)
(reduce dissoc todos))))
;; -- Subscriptions ---------
(register-sub
:initialised?
(fn
@ -145,11 +145,8 @@
:visible-todos
(fn
[db _]
(let [todos (reaction (:todos @db))
showing (reaction (:showing @db))]
(reaction (filter (filter-fn-for @showing)
(vals @todos))))))
(reaction (filter (filter-fn-for (:showing @db))
(vals (:todos @db))))))
(register-sub
:completed-count
@ -181,7 +178,6 @@
[active-count completed-count showing])))) ;; tuple
;; -- Components ---------
@ -268,7 +264,7 @@
[:input#toggle-all
{:type "checkbox"
:checked (pos? @completed-count)
:on-change #(dispatch [:complete-all (pos? @completed-count)])}]
:on-change #(dispatch [:complete-all-toggle])}]
[:label {:for "toggle-all"} "Mark all as complete"]
[todo-list visible-todos]]
[stats-footer]])]