Better comments and naming for todomvc

This commit is contained in:
mike-thompson-day8 2015-05-02 10:45:32 +10:00
parent 7d85dbca48
commit c07730d794

View File

@ -7,10 +7,12 @@
;; -- Middleware -------------------------------------------------------------- ;; -- Middleware --------------------------------------------------------------
;; ;;
(def ->ls (after todos->ls!)) ;; middleware to put todos into local storage (def ->ls (after todos->ls!)) ;; middleware to store todos into local storage
;; middleware for any handler which manipulates todos. ;; middleware for any handler that manipulates todos
(def todo-ware [(path :todos) ->ls trim-v]) (def todo-middleware [(path :todos) ;; 1st param to handler will be value from this path
->ls ;; write to localstore each time
trim-v]) ;; remove event id from event vec
;; -- Helpers ----------------------------------------------------------------- ;; -- Helpers -----------------------------------------------------------------
@ -22,23 +24,29 @@
;; -- Handlers ---------------------------------------------------------------- ;; -- Handlers ----------------------------------------------------------------
(register-handler ;; disptached to on app startup ;; usage: (dispatch [:initialise-db])
(register-handler ;; On app startup, ceate initial state
:initialise-db ;; event id being handled :initialise-db ;; event id being handled
(fn [_ _] ;; the handler (fn [_ _] ;; the handler being registered
(merge default-value (ls->todos)))) ;; all hail the new state (merge default-value (ls->todos)))) ;; all hail the new state
(register-handler ;; handlers changes the footer filter (register-handler ;; this handler changes the footer filter
:set-showing ;; event-id :set-showing ;; event-id
[(path :showing) trim-v] ;; middleware (wraps the handler) [(path :showing) trim-v] ;; middleware (wraps the handler)
(fn ;; handler
[db [filter-kw]] ;; Because of the path middleware above, the 1st parameter to
filter-kw)) ;; the handler below won't be the entire 'db', and instead will
;; be the value at a certain path within db.
;; Also, the use of the 'trim-v' middleware means we can omit
;; the leading underscore from the 2nd parameter (event vector).
(fn [old-kw [new-filter-kw]] ;; handler
new-filter-kw)) ;; return new state for the path
(register-handler ;; given the text, create a new todo (register-handler ;; given the text, create a new todo
:add-todo :add-todo
todo-ware todo-middleware
(fn [todos [text]] ;; "path" middlware means we are given :todo (fn [todos [text]] ;; "path" middlware means we are given :todo
(let [id (next-id todos)] (let [id (next-id todos)]
(assoc todos id {:id id :title text :done false})))) (assoc todos id {:id id :title text :done false}))))
@ -46,38 +54,38 @@
(register-handler (register-handler
:toggle-done :toggle-done
todo-ware todo-middleware
(fn [todos [id]] (fn [todos [id]]
(update-in todos [id :done] not))) (update-in todos [id :done] not)))
(register-handler (register-handler
:save :save
todo-ware todo-middleware
(fn [todos [id title]] (fn [todos [id title]]
(assoc-in todos [id :title] title))) (assoc-in todos [id :title] title)))
(register-handler (register-handler
:delete-todo :delete-todo
todo-ware todo-middleware
(fn [todos [id]] (fn [todos [id]]
(dissoc todos id))) (dissoc todos id)))
(register-handler (register-handler
:clear-completed :clear-completed
todo-ware todo-middleware
(fn [todos _] (fn [todos _]
(->> (vals todos) ;; remove all todos where :done is true (->> (vals todos) ;; remove all todos where :done is true
(filter :done) (filter :done)
(map :id) (map :id)
(reduce dissoc todos)))) (reduce dissoc todos)))) ;; returns the new version of todos
(register-handler (register-handler
:complete-all-toggle :complete-all-toggle
todo-ware todo-middleware
(fn [todos] (fn [todos]
(let [new-done (not-every? :done (vals todos))] ;; toggle true or false? (let [new-done (not-every? :done (vals todos))] ;; toggle true or false?
(reduce #(assoc-in %1 [%2 :done] new-done) (reduce #(assoc-in %1 [%2 :done] new-done)