Skip to content

Commit fe73702

Browse files
committed
Counter with parser
1 parent 69cbe03 commit fe73702

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

src/om_tutorial/core.cljs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,38 @@
33
[om.next :as om :refer-macros [defui]]
44
[om.dom :as dom]))
55

6-
(enable-console-print!)
7-
86
(def app-state (atom {:count 0}))
97

8+
(defn read [{:keys [state] :as env} key params]
9+
(let [st @state]
10+
(if-let [[_ value] (find st key)]
11+
{:value value}
12+
{:value :not-found})))
13+
14+
(defn mutate [{:keys [state] :as env} key params]
15+
(if (= 'increment key)
16+
{:value {:keys [:count]}
17+
:action #(swap! state update-in [:count] inc)}
18+
{:value :not-found}))
19+
1020
(defui Counter
21+
static om/IQuery
22+
(query [this]
23+
[:count])
1124
Object
1225
(render [this]
1326
(let [{:keys [count]} (om/props this)]
1427
(dom/div nil
1528
(dom/span nil (str "Count: " count))
1629
(dom/button
1730
#js {:onClick
18-
(fn [e]
19-
(swap! app-state update-in [:count] inc))}
31+
(fn [e] (om/transact! this '[(increment)]))}
2032
"Click me!")))))
2133

2234
(def reconciler
23-
(om/reconciler {:state app-state}))
35+
(om/reconciler
36+
{:state app-state
37+
:parser (om/parser {:read read :mutate mutate})}))
2438

2539
(om/add-root! reconciler
2640
Counter (gdom/getElement "app"))

0 commit comments

Comments
 (0)