Skip to content

Instantly share code, notes, and snippets.

@claudiuapetrei
Created May 9, 2017 14:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save claudiuapetrei/5baa8859fc1f6e7837d5e4aed5e8ca50 to your computer and use it in GitHub Desktop.
Save claudiuapetrei/5baa8859fc1f6e7837d5e4aed5e8ca50 to your computer and use it in GitHub Desktop.
(def app-state-5 (atom {:a {:key :a :count 13} :b {:key :b :count 33}}))
(defmulti read om/dispatch)
(defmulti mutate om/dispatch)
(defmethod read :default [{:keys [state] :as env} key params]
(let [st @state]
(if-let [[_ value] (find st key)]
{:value value}
{:value :not-found})))
(defmethod mutate 'increment [{:keys [state] :as env} key params]
(let [update-key (:key params)]
{:value {:keys [:count]}
:action #(swap! state update-in [update-key :count] inc)}))
(defui Counter
static om/IQuery
(query [this]
[:key :count])
Object
(render [this]
(let [{:keys [count key]} (om/props this)]
(dom/div nil
(dom/span nil (str "Count: " count))
(dom/br nil nil)
(dom/button
#js {:onClick
(fn [e] (om/transact! this `[(~'increment {:key ~key})]))}
"Click me!")))))
(def button-comp (om/factory Counter))
(defui CounterGroup
static om/IQuery
(query [this] [{:a (om/get-query Counter)} {:b (om/get-query Counter)}])
Object
(render [this]
(let [st (om/props this) {:keys [count a b]} st]
(dom/div nil
(button-comp a)
(button-comp b)))))
(def reconciler-5
(om/reconciler
{:state app-state-5
:parser (om/parser {:read read :mutate mutate})}))
(om/add-root! reconciler-5
CounterGroup (gdom/getElement "app"))
(defn log-app-state []
(let [st @(om/app-state reconciler-5)]
(binding [*print-namespace-maps* false] (pprint st))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment