从 re-frame 开始并且有一个(可能非常基本的)混淆点。
我有一个订阅:
(defn get-vote-by-id
[votes id]
(filterv #(= (:id %) id) votes))
(register-sub
:cvs
(fn cvs-sub [db [_]]
(make-reaction
(fn cvs-sub-reaction []
(let [cv (get-in @db [:current-vote])
votes (get-in @db [:votes])]
;; why is this evaluating to []?
(get-vote-by-id votes cv))))))
此 Form-2 组件使用:
(defn vote-page []
(let [ready? (subscribe [:current-vote-initialised?])
current-vote-id (subscribe [:current-vote-id-sub])
cv (subscribe [:cvs])]
(fn vote-page-renderer []
[:div
[:h2 "Vote"]
(if @ready?
(do
[:div
[:div (str "cv: " @cv)]
[:div (str "Current Vote id: " @current-vote-id)]])
[:div "Initialising..."])])))
我的 app-db 有有效数据,{:votes ... 是地图向量,每个地图都有一个 :id 参数,{:current-vote 是一个数字。
我知道 get-vote-by-id 有效:
(get-vote-by-id [{:id 1 :data "hi"} {:id 2 :data "there"}] 2)
[{:id 2, :data "there"}]
如果我将 (get-vote-by-id votes cv) 中的 cv 替换为一个常数:(get-vote-by-id votes 2),那么它就可以工作。但除此之外, (get-vote-by-id ...) 正在评估一个空向量 []。
任何帮助表示赞赏,并提前感谢!
这表明 get-vote-by-id 传递了正确的值(用于投票的向量和用于 id 的数字):