Skip to content

Instantly share code, notes, and snippets.

@claudiuapetrei
Created May 9, 2017 10:52
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/9743ae25f39452bd68565ae9c55853cb to your computer and use it in GitHub Desktop.
Save claudiuapetrei/9743ae25f39452bd68565ae9c55853cb to your computer and use it in GitHub Desktop.
(def app-state-5 (atom {:count 0 :a {:count 13} :b {:count 33}}))
(defn read [{:keys [state] :as env} key params]
(let [st @state]
(if-let [[_ value] (find st key)]
{:value value}
{:value :not-found})))
(defn mutate [{:keys [state] :as env} key params]
(prn "params" params (:update-key params))
(if (= 'increment key)
{:value nil
:action #(swap! state update-in (:update-key params) inc)}
{:value :not-found}))
(defui Counter
static om/IQuery
(query [this]
[:count])
Object
(render [this]
(let [{:keys [count]} (om/props this)
{:keys [update-inc]} (om/get-computed this)]
(prn "count?" count)
(dom/div nil
(dom/span nil (str "Count: " count))
(dom/br nil nil)
(dom/button
#js {:onClick
update-inc}
"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)} :count])
Object
(render [this]
(let [st (om/props this) {:keys [count a b]} st]
(prn "st" st)
(dom/div nil
(button-comp (om/computed a {:update-inc (fn [] (om/transact! this `[(~'increment {:update-key [:a :count]}) ]))}))
(button-comp (om/computed b {:update-inc (fn [] (om/transact! this `[(~'increment {:update-key [:b :count]})]))}))
(button-comp (om/computed {:count count} {:update-inc (fn [] (om/transact! this `[(~'increment {:update-key [:count]})]))}))))))
(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"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment