Transact app-state in will-mount has no effect

This question is best explained with an example:

;; create a basic om app.
lein new mies-om om-tut
lein cljsbuild auto.

      

Then paste the following code (in core.cljs

)

(ns om-tut.core
  (:require [om.core :as om :include-macros true]
    [om.dom :as dom :include-macros true]))

(def app-state (atom {:text "Hello world!"}))

(om/root
  (fn [app owner]
    (reify
      om/IWillMount
      (will-mount [_]
          (om/update! app :text "Success!!!"))
      om/IRender
      (render [_]
         (dom/div nil (app :text ))
        )))
  app-state
  {:target (. js/document (getElementById "app"))})

      

The code is will-mount

actually executed, if you run the function println

, you will see it. It is not clear why the render loop is called only once. On the other hand, if you wrap om/update!

in a block go

, then it works as expected:

;; add [org.clojure/core.async "0.1.346.0-17112a-alpha"] to your deps in project.clj
(ns om-tut.core
  (:require-macros [cljs.core.async.macros :refer [go]])
  (:require [om.core :as om :include-macros true]
                [cljs.core.async :refer [put! chan <! to-chan close!]]
                [om.dom :as dom :include-macros true]))

(def app-state (atom {:text "Hello world!"}))

(om/root
  (fn [app owner]
    (reify
      om/IWillMount
      (will-mount [_]
          (go 
            (om/update! app :text "Success!!")))
      om/IRender
      (render [_]
           (dom/div nil (app :text )))))
  app-state
  {:target (. js/document (getElementById "app"))})

      

This begs the question: why will-mount

doesn't it start a new render loop as I update the state of the application? I like to use blocks go

when I need them, but I don't understand why I have to wrap this simple example into a block.

+3


source to share


1 answer


He thinks that will-mount is not a good place to update a cursor. Calling om / build with the option: fn will do what you are trying to achieve.

The component is rendered only once with an updated cursor.



(om/build mycomponent data {:fn #(assoc % :text "Success !")})

https://github.com/swannodette/om/wiki/Documentation#build

+1


source







All Articles