重新框架:订阅无法按预期工作

时间:2016-06-10 14:44:40

标签: clojurescript 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 2)替换cv(get-vote-by-id votes cv),那么它就可以了。但除此之外,(get-vote-by-id ...)正在评估空向量[]。

任何帮助表示赞赏,并提前致谢!

这表明get-vote-by-id传递正确的值(投票的矢量和id的数字):

get-vote-by-id

1 个答案:

答案 0 :(得分:2)

经过一些调试后,我们发现问题还在上游。根本原因是id被设置为字符串。将id强制转换为整数解决了问题。

相关问题