Created
May 9, 2017 10:00
-
-
Save claudiuapetrei/b8b8d3d33544aa42d618485c6484d978 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(def app-state-5 (atom {:count 0 :count-a 13 :count-b 33})) | |
(defmulti app-read om/dispatch) | |
(defmethod app-read :default [{:keys [state] :as env} key params] | |
(prn "read?" key) | |
(let [st @state] | |
(if-let [[_ value] (find st key)] | |
{:value value} | |
{:value :not-found}))) | |
(defn mutate [{:keys [state] :as env} key params] | |
(let [update-key (:update-key params)] | |
(prn key update-key) | |
(if (= 'increment key) | |
{:value nil | |
:action #(swap! state update-in [update-key] inc)} | |
{:value :not-found}))) | |
(defui Counter | |
static om/IQuery | |
(query [this] | |
[:name :count]) | |
Object | |
(render [this] | |
(let [{:keys [count name]} (om/props this)] | |
(dom/div nil | |
(dom/span nil (str "Count: " count)) | |
(dom/br nil nil) | |
(dom/button | |
#js {:onClick | |
(fn [e] (om/transact! (om/get-reconciler this) `[(~'increment {:update-key ~name})]))} | |
"Click me!"))))) | |
(def button-comp (om/factory Counter)) | |
(defui CounterGroup | |
static om/IQuery | |
(query [this] [:count-a :count-b :count]) | |
Object | |
(render [this] | |
(let [st (om/props this) {:keys [count count-a count-b]} st] | |
(dom/div nil | |
(button-comp {:name :count :count count}) | |
(button-comp {:name :count-a :count count-a}) | |
(button-comp {:name :count-b :count count-b}))))) | |
(def reconciler-5 | |
(om/reconciler | |
{:state app-state-5 | |
:parser (om/parser {:read app-read :mutate mutate})})) | |
(om/add-root! reconciler-5 | |
CounterGroup (gdom/getElement "app")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment