2015-02-25 01:31:11 +00:00
|
|
|
(ns simpleexample.core
|
|
|
|
(:require-macros [reagent.ratom :refer [reaction]])
|
|
|
|
(:require [reagent.core :as reagent :refer [atom]]
|
2015-02-25 22:38:22 +00:00
|
|
|
[re-frame.core :refer [register-handler
|
|
|
|
register-pure-handler
|
|
|
|
pure
|
2015-02-26 03:23:03 +00:00
|
|
|
register-sub
|
2015-02-25 22:38:22 +00:00
|
|
|
dispatch
|
|
|
|
subscribe]]))
|
2015-02-25 01:31:11 +00:00
|
|
|
|
|
|
|
(defonce timer (atom (js/Date.)))
|
|
|
|
|
|
|
|
(defonce time-updater (js/setInterval
|
|
|
|
#(dispatch [:timer (js/Date.)]) 1000))
|
|
|
|
|
2015-02-25 22:38:22 +00:00
|
|
|
;;; this is the initial state
|
2015-02-25 01:31:11 +00:00
|
|
|
(defonce initial-state
|
|
|
|
{:timer (js/Date.)
|
|
|
|
:time-color "#f34"})
|
|
|
|
|
2015-02-25 22:38:22 +00:00
|
|
|
;; Handlers
|
|
|
|
;-------------------------------------------------------------
|
2015-02-25 01:31:11 +00:00
|
|
|
|
2015-02-25 22:38:22 +00:00
|
|
|
;; This handler sets the initial state
|
|
|
|
(register-pure-handler
|
|
|
|
;; register-pure-handler is a convience macro that will be used 90% of the
|
|
|
|
;; time
|
|
|
|
;; the handler is passed a map (not an atom) and must return the new state
|
|
|
|
;; of the db
|
|
|
|
:initialize
|
2015-02-25 01:31:11 +00:00
|
|
|
(fn
|
|
|
|
[db _]
|
2015-02-25 22:38:22 +00:00
|
|
|
(merge db initial-state)))
|
2015-02-25 01:31:11 +00:00
|
|
|
|
2015-02-25 22:38:22 +00:00
|
|
|
;; This handler changes the color of the displayed time
|
|
|
|
(register-handler
|
|
|
|
;;; register-handler can take 3 arguments to allow you to insert middleware
|
|
|
|
;;; see https://github.com/Day8/re-frame/wiki/Handler-Middleware
|
2015-02-25 01:31:11 +00:00
|
|
|
:time-color
|
2015-02-25 22:38:22 +00:00
|
|
|
pure
|
2015-02-25 01:31:11 +00:00
|
|
|
(fn
|
|
|
|
;; the first item in the second argument is :time-color the second is the
|
|
|
|
;; new value
|
|
|
|
[db [_ value]]
|
2015-02-25 22:38:22 +00:00
|
|
|
(assoc db :time-color value)))
|
2015-02-25 01:31:11 +00:00
|
|
|
|
2015-02-25 22:38:22 +00:00
|
|
|
;; This handler changes the value of the time
|
|
|
|
(register-handler
|
|
|
|
;; This is a non-pure handler (NOT RECCOMENDED), no middleware is called
|
|
|
|
;; and the app-db is passed directly as a atom.
|
|
|
|
;; Note, there is no reason why this particular handler should not be pure
|
2015-02-25 01:31:11 +00:00
|
|
|
:timer
|
|
|
|
(fn
|
|
|
|
;; the first item in the second argument is :timer the second is the
|
|
|
|
;; new value
|
|
|
|
[db [_ value]]
|
|
|
|
(swap! db assoc :timer value)))
|
|
|
|
|
2015-02-25 22:38:22 +00:00
|
|
|
;; add subscriptions to :timer and :time-color
|
2015-02-26 03:23:03 +00:00
|
|
|
(register-sub
|
2015-02-25 22:38:22 +00:00
|
|
|
:timer
|
|
|
|
(fn
|
|
|
|
[db _]
|
|
|
|
;; you need to wrap your subscription code in a reaction
|
|
|
|
(reaction (:timer @db))))
|
|
|
|
|
2015-02-26 03:23:03 +00:00
|
|
|
(register-sub
|
2015-02-25 22:38:22 +00:00
|
|
|
:time-color
|
|
|
|
(fn
|
|
|
|
[db _]
|
|
|
|
;; you need to wrap your subscription code in a reaction
|
|
|
|
(reaction (:time-color @db))))
|
|
|
|
|
2015-02-25 01:31:11 +00:00
|
|
|
(dispatch [:initialize])
|
|
|
|
|
|
|
|
|
|
|
|
(defn greeting [message]
|
|
|
|
[:h1 message])
|
|
|
|
|
|
|
|
(defn clock []
|
2015-02-25 22:38:22 +00:00
|
|
|
(let [time-color (subscribe [:time-color])
|
|
|
|
timer (subscribe [:timer])]
|
2015-02-25 01:31:11 +00:00
|
|
|
;;; wrap your component in a function to use the suscription
|
|
|
|
(fn []
|
|
|
|
;; note that the initialize call will not be dispatched immediately
|
|
|
|
;; as it is an async call
|
|
|
|
(when @timer
|
|
|
|
(let [time-str (-> @timer .toTimeString (clojure.string/split " ") first)]
|
|
|
|
[:div.example-clock
|
|
|
|
{:style {:color @time-color}}
|
|
|
|
time-str])))))
|
|
|
|
|
|
|
|
(defn color-input []
|
2015-02-25 22:38:22 +00:00
|
|
|
(let [time-color (subscribe [:time-color])]
|
2015-02-25 01:31:11 +00:00
|
|
|
;;; wrap your component in a function to use the suscription
|
|
|
|
(fn []
|
|
|
|
[:div.color-input
|
|
|
|
"Time color: "
|
|
|
|
[:input {:type "text"
|
|
|
|
:value @time-color
|
|
|
|
:on-change #(dispatch
|
|
|
|
[:time-color (-> % .-target .-value)])}]])))
|
|
|
|
|
|
|
|
(defn simple-example []
|
|
|
|
[:div
|
|
|
|
[greeting "Hello world, it is now"]
|
|
|
|
[clock]
|
|
|
|
[color-input]])
|
|
|
|
|
|
|
|
(defn ^:export run []
|
|
|
|
(reagent/render [simple-example]
|
|
|
|
(js/document.getElementById "app")))
|