This commit is contained in:
Mike Thompson 2017-07-16 19:50:00 +10:00
commit 0901c8ffc4
3 changed files with 22 additions and 23 deletions

View File

@ -1,6 +1,7 @@
(ns simple.core (ns simple.core
(:require [reagent.core :as reagent] (:require [reagent.core :as reagent]
[re-frame.core :as rf])) [re-frame.core :as rf]
[clojure.string :as str]))
;; A detailed walk-through of this source code is provied in the docs: ;; A detailed walk-through of this source code is provied in the docs:
;; https://github.com/Day8/re-frame/blob/master/docs/CodeWalkthrough.md ;; https://github.com/Day8/re-frame/blob/master/docs/CodeWalkthrough.md
@ -13,7 +14,7 @@
(rf/dispatch [:timer now]))) ;; <-- dispatch used (rf/dispatch [:timer now]))) ;; <-- dispatch used
;; Call the dispatching function every second. ;; Call the dispatching function every second.
;; `defonce` is like `def` but it ensures only instance is ever ;; `defonce` is like `def` but it ensures only one instance is ever
;; created in the face of figwheel hot-reloading of this file. ;; created in the face of figwheel hot-reloading of this file.
(defonce do-timer (js/setInterval dispatch-timer-event 1000)) (defonce do-timer (js/setInterval dispatch-timer-event 1000))
@ -44,8 +45,7 @@
(rf/reg-sub (rf/reg-sub
:time :time
(fn [db _] ;; db is current app state. 2nd unused param is query vector (fn [db _] ;; db is current app state. 2nd unused param is query vector
(-> db (:time db))) ;; return a query computation over the application state
:time)))
(rf/reg-sub (rf/reg-sub
:time-color :time-color
@ -61,7 +61,7 @@
{:style {:color @(rf/subscribe [:time-color])}} {:style {:color @(rf/subscribe [:time-color])}}
(-> @(rf/subscribe [:time]) (-> @(rf/subscribe [:time])
.toTimeString .toTimeString
(clojure.string/split " ") (str/split " ")
first)]) first)])
(defn color-input (defn color-input
@ -86,4 +86,3 @@
(rf/dispatch-sync [:initialize]) ;; puts a value into application state (rf/dispatch-sync [:initialize]) ;; puts a value into application state
(reagent/render [ui] ;; mount the application's ui into '<div id="app" />' (reagent/render [ui] ;; mount the application's ui into '<div id="app" />'
(js/document.getElementById "app"))) (js/document.getElementById "app")))

View File

@ -1,26 +1,27 @@
(ns todomvc.views (ns todomvc.views
(:require [reagent.core :as reagent] (:require [reagent.core :as reagent]
[re-frame.core :refer [subscribe dispatch]])) [re-frame.core :refer [subscribe dispatch]]
[clojure.string :as str]))
(defn todo-input [{:keys [title on-save on-stop]}] (defn todo-input [{:keys [title on-save on-stop]}]
(let [val (reagent/atom title) (let [val (reagent/atom title)
stop #(do (reset! val "") stop #(do (reset! val "")
(when on-stop (on-stop))) (when on-stop (on-stop)))
save #(let [v (-> @val str clojure.string/trim)] save #(let [v (-> @val str str/trim)]
(when (seq v) (on-save v)) (when (seq v) (on-save v))
(stop))] (stop))]
(fn [props] (fn [props]
[:input (merge props [:input (merge props
{:type "text" {:type "text"
:value @val :value @val
:auto-focus true :auto-focus true
:on-blur save :on-blur save
:on-change #(reset! val (-> % .-target .-value)) :on-change #(reset! val (-> % .-target .-value))
:on-key-down #(case (.-which %) :on-key-down #(case (.-which %)
13 (save) 13 (save)
27 (stop) 27 (stop)
nil)})]))) nil)})])))
(defn todo-item (defn todo-item

View File

@ -16,9 +16,9 @@
"Create an interceptor from named arguments" "Create an interceptor from named arguments"
[& {:as m :keys [id before after]}] [& {:as m :keys [id before after]}]
(when debug-enabled? (when debug-enabled?
(if-let [unknown-keys (seq (clojure.set/difference (if-let [unknown-keys (seq (set/difference
(-> m keys set) (-> m keys set)
mandatory-interceptor-keys))] mandatory-interceptor-keys))]
(console :error "re-frame: ->interceptor " m " has unknown keys:" unknown-keys))) (console :error "re-frame: ->interceptor " m " has unknown keys:" unknown-keys)))
{:id (or id :unnamed) {:id (or id :unnamed)
:before before :before before
@ -195,4 +195,3 @@
(invoke-interceptors :before) (invoke-interceptors :before)
change-direction change-direction
(invoke-interceptors :after))) (invoke-interceptors :after)))