Om Next tutorial: calling get-people function correctly

I am currently following the next om-next tutorial. The Add Reads section defines a function get-people. Along with this function, a map is defined init-datathat contains a list of people.

(defn get-people [state key]
  (let [st @state]
    (into [] (map #(get-in st %)) (get st key))))

(def init-data {:list/one
 [{:name "John", :points 0}
  {:name "Mary", :points 0}
  {:name "Bob", :points 0}],
 :list/two
 [{:name "Mary", :points 0, :age 27}
  {:name "Gwen", :points 0} 
  {:name "Jeff", :points 0}]})

Here is my attempt to call this function.

(get-people (atom init-data) :list/one) ;; => [nil nil nil]

As you can see, I'm just returning a vector nil. I do not quite understand what I should call this function. Can anyone help me out? Thanks!

+4
source share
1 answer

Ok, I figured it out.

init-data get-people . "", Om reconciler. Normalization.

init-data, deref :

{:list/one
 [[:person/by-name "John"]
  [:person/by-name "Mary"]
  [:person/by-name "Bob"]],
 :list/two
 [[:person/by-name "Mary"]
  [:person/by-name "Gwen"]
  [:person/by-name "Jeff"]],
 :person/by-name
 {"John" {:name "John", :points 0},
  "Mary" {:name "Mary", :points 0, :age 27},
  "Bob" {:name "Bob", :points 0},
  "Gwen" {:name "Gwen", :points 0},
  "Jeff" {:name "Jeff", :points 0}}}

get-people init-data:

; reconciled initial data
(def reconciled-data
  {:list/one
   [[:person/by-name "John"]
    [:person/by-name "Mary"]
    [:person/by-name "Bob"]],
   :list/two
   [[:person/by-name "Mary"]
    [:person/by-name "Gwen"]
    [:person/by-name "Jeff"]],
   :person/by-name
   {"John" {:name "John", :points 0},
    "Mary" {:name "Mary", :points 0, :age 27},
    "Bob" {:name "Bob", :points 0},
    "Gwen" {:name "Gwen", :points 0},
    "Jeff" {:name "Jeff", :points 0}}}

; correct function call
(get-people (atom reconciled-data) :list/one)

; returned results
[{:name "John", :points 0}
 {:name "Mary", :points 0, :age 27}
 {:name "Bob", :points 0}]

:

  • , :list/one. ( ).
  • , . (get-in st [:person/by-name "John"]) {:name "John", :points 0}.

- , , .

+5

Source: https://habr.com/ru/post/1614216/


All Articles