Om / Clojurescript:问题呈现重置应用程序状态

时间:2015-06-14 09:01:34

标签: clojurescript om

我正在尝试使用om显示一个组件,只要此小部件所需的数据到达。我提出了以下(大致):

(def data (atom     {}))                                                               

(go (let [response (<! (http/get "../rest/ds" ))]                               
  (reset! data (:result (:body response)))))

(om/root
  (fn [app owner]                                                                  
    (reify
     om/IInitState
     (init-state [_]
       (prn "(1) returning initial state now")
       {:text "Hello world!"})
     om/IRenderState 
     (render-state [this state]                                                   
        (prn state) ; <-- here: not the state that I reset! before but the original state
        (do-something ...))))                                                  
  data
  {:target (. js/document (getElementById "app"))})

state中的render-state似乎永远不是我用reset!设置的@data,尽管似乎会触发重新渲染。我在这里用错了吗?直接访问render-state中的reset!时,它会显示我使用push()设置的正确值。

1 个答案:

答案 0 :(得分:1)

(prn state)中的状态是组件状态。原子data是应用程序状态,在您的组件中,您调用了app

(om/root
  (fn [app owner]  ; <- `app` is the application state                                                              
    (reify
     om/IInitState
     (init-state [_]
       (prn "(1) returning initial state now")
       {:text "Hello world!"})
     om/IRenderState 
     (render-state [this state]                                                   
        (prn app) ; <- refer to application state instead
        (do-something ...))))                          
  data
  {:target (. js/document (getElementById "app"))})