Skip to content

Instantly share code, notes, and snippets.

@claudiuapetrei
Created May 9, 2017 10:00
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/b8b8d3d33544aa42d618485c6484d978 to your computer and use it in GitHub Desktop.
Save claudiuapetrei/b8b8d3d33544aa42d618485c6484d978 to your computer and use it in GitHub Desktop.
(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