好吧,我想通了。
init-data
不是要调用的正确数据结构get-people
。初始数据必须首先使用 Om's 进行“协调” reconciler
。您可以在本教程的规范化部分找到有关协调器的更多信息。
协调init-data
映射然后deref
ing 数据返回这个规范化的数据结构:
{: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}
。
- 将结果作为向量返回
如果有人正在阅读本文并希望进一步澄清,请告诉我。