Search code examples
clojurescriptom

Om/Clojurescript: Issue rendering a reset application state


I am trying to display a component with om, as soon as the data needed for this widget arrives. I came up with the following (roughly):

(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"))})

It seems that state within render-state is never what I set it with reset! although a re-rendering seems to be triggered. Am I using something wrong here? when directly accessing @data in render-state it shows the proper value that I set with reset!.


Solution

  • The state in (prn state) is the component state. The atom data is the application state, which in your component you have called 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"))})