Bootstrap todomvc correctly.

This commit is contained in:
mike-thompson-day8 2015-03-02 07:58:07 +11:00
parent 6a3f2e4b74
commit 497926e4b4
1 changed files with 27 additions and 10 deletions

View File

@ -5,7 +5,7 @@
register-sub register-sub
subscribe subscribe
dispatch dispatch
path trim-v undoable]])) path trim-v debug]]))
;; TODOs ;; TODOs
;; add .gitignore ;; add .gitignore
@ -17,9 +17,9 @@
;; change example.html to todo.html ;; change example.html to todo.html
;; add middleware to save to local storage - beware using ;; add middleware to save to local storage - beware using
;; check that (path ) does an identity test before swaping back in ;; check that (path ) does an identity test before swaping back in
;; add apply-vector middleware
;; -- Helpers ------------- ;; -- Helpers -------------
(enable-console-print!)
(defn next-id (defn next-id
[todos] [todos]
@ -27,7 +27,6 @@
0 0
(inc (apply max (keys todos))))) ;; hillariously inefficient, but yeah (inc (apply max (keys todos))))) ;; hillariously inefficient, but yeah
;; -- Middleware ------------- ;; -- Middleware -------------
;; ;;
;; Handlers can be wrapped in middleware. ;; Handlers can be wrapped in middleware.
@ -51,7 +50,7 @@
;; ;;
;; Middleware means our handlers are pure and simple. ;; Middleware means our handlers are pure and simple.
;; ;;
(def todo-middleware (comp undoable (path [:todos]) trim-v)) (def todo-middleware (comp (path [:todos]) trim-v))
;; -- Handlers ------------- ;; -- Handlers -------------
@ -62,12 +61,11 @@
(fn (fn
[_ _] [_ _]
{:todos (sorted-map) {:todos (sorted-map)
:showing :all} :showing :all}))
#_(merge db initial-db)))
(register-pure-handler (register-pure-handler
:set-showing :set-showing
(comp undoable trim-v) ;; middleware trim-v
(fn (fn
[db [filter-kw]] [db [filter-kw]]
(assoc db :showing filter-kw))) (assoc db :showing filter-kw)))
@ -124,12 +122,21 @@
;; -- Subscriptions --------- ;; -- Subscriptions ---------
(register-sub
:initialised?
(fn
[db _]
(reaction (not= {} @db))))
(register-sub (register-sub
:visible-todos :visible-todos
(fn (fn
[db _] [db _]
(let [todos (reaction (:todos @db)) (let [todos (reaction (:todos @db))
showing (reaction (:showing @db)) showing (reaction (:showing @db))
_ (println @showing)
filter-fn (case @showing filter-fn (case @showing
:active (complement :done) :active (complement :done)
:done :done :done :done
@ -181,7 +188,7 @@
{:component-did-mount #(.focus (reagent/dom-node %))})) {:component-did-mount #(.focus (reagent/dom-node %))}))
(defn stats-footer [] (defn stats-footer []
(let [footer-stats (subscribe :footer-stats)] (let [footer-stats (subscribe [:footer-stats])]
(fn [] (fn []
(let [[active done filter] @footer-stats (let [[active done filter] @footer-stats
props-for (fn [filter-kw] props-for (fn [filter-kw]
@ -245,7 +252,17 @@
[:footer#info [:footer#info
[:p "Double-click to edit a todo"]]]))) [:p "Double-click to edit a todo"]]])))
(defn ^:export run (defn main-panel
[]
(let [initialised? (subscribe [:initialised?])]
(fn []
(if @initialised?
[todo-app]
[:div "Loading ...."]))))
(defn ^:export main
[] []
(dispatch [:initialise-db]) (dispatch [:initialise-db])
(reagent/render [todo-app] (js/document.getElementById "app"))) (reagent/render [main-panel] (js/document.getElementById "app")))
;; you must always return the db