Skip to content

Commit 8d77e34

Browse files
committed
AnimalsList
1 parent fe73702 commit 8d77e34

File tree

1 file changed

+27
-17
lines changed

1 file changed

+27
-17
lines changed

src/om_tutorial/core.cljs

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

6-
(def app-state (atom {:count 0}))
6+
(def app-state
7+
(atom
8+
{:app/title "Animals"
9+
:animals/list
10+
[[1 "Ant"] [2 "Antelope"] [3 "Bird"] [4 "Cat"] [5 "Dog"]
11+
[6 "Lion"] [7 "Mouse"] [8 "Monkey"] [9 "Snake"] [10 "Zebra"]]}))
712

8-
(defn read [{:keys [state] :as env} key params]
13+
(defmulti read (fn [env key params] key))
14+
15+
(defmethod read :default
16+
[{:keys [state] :as env} key params]
917
(let [st @state]
1018
(if-let [[_ value] (find st key)]
1119
{:value value}
1220
{:value :not-found})))
1321

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}))
22+
(defmethod read :animals/list
23+
[{:keys [state] :as env} key {:keys [start end]}]
24+
{:value (subvec (:animals/list @state) start end)})
1925

20-
(defui Counter
26+
(defui AnimalsList
27+
static om/IQueryParams
28+
(params [this]
29+
{:start 0 :end 10})
2130
static om/IQuery
2231
(query [this]
23-
[:count])
32+
'[:app/title (:animals/list {:start ?start :end ?end})])
2433
Object
2534
(render [this]
26-
(let [{:keys [count]} (om/props this)]
35+
(let [{:keys [app/title animals/list]} (om/props this)]
2736
(dom/div nil
28-
(dom/span nil (str "Count: " count))
29-
(dom/button
30-
#js {:onClick
31-
(fn [e] (om/transact! this '[(increment)]))}
32-
"Click me!")))))
37+
(dom/h2 nil title)
38+
(apply dom/ul nil
39+
(map
40+
(fn [[i name]]
41+
(dom/li nil (str i ". " name)))
42+
list))))))
3343

3444
(def reconciler
3545
(om/reconciler
3646
{:state app-state
37-
:parser (om/parser {:read read :mutate mutate})}))
47+
:parser (om/parser {:read read})}))
3848

3949
(om/add-root! reconciler
40-
Counter (gdom/getElement "app"))
50+
AnimalsList (gdom/getElement "app"))

0 commit comments

Comments
 (0)