我目前正在试验 om 并尝试加载外部数据以在组件中显示它。
我的详细信息组件:
(defn detail-component [app owner opts]
(reify
om/IInitState
(init-state [_]
(om/transact! app [:data] (fn [] "Test")))
om/IWillMount
(will-mount [_]
(go (let [foo (<! (fetch-something 1))]
(om/update! app #(assoc % :data foo))
)))
om/IRender
(render [_]
(dom/div nil
(dom/h1 nil "Foo")
(om/build another-component app)
)
))
)
(fetch-something
正在从 API 检索数据)。
(defn another-component [{:keys [data]}]
(om/component
(.log js/console data)
(dom/h2 nil "Another component goes here")
(dom/h2 nil (data :description))
)
)
总而言之,detail-component 在挂载之前获取数据,将其附加到app
并构建another-component
。another-component
然后从该数据中取出描述并显示它。
但是,在执行时,Uncaught TypeError: Cannot read property 'call' of null
我正试图访问描述。这向我表明,在another-component
构建时,数据还不存在并且它失败了。
当数据可用时,我如何告诉 om 构建应用程序?还是我必须建立一些nil?
检查?