Search code examples
clojurescriptom

transact app-state in will-mount has no effect


This question can be best explained with an example:

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

Then paste in 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 in will-mount is actually being executed, if you drop in a println function, then you'll see that. What is not clear is why the rendering loop is called only once. On the other hand, if you wrap the om/update! within a go block, 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"))})

The question is: Why does will-mount not trigger a new rendering loop, since I update app state? I like to use go blocks when I need them, but I don't see why I am forced to wrap this simple example in a block.


Solution

  • It think that will-mount is not a good place to update cursor. Calling om/build with the :fn option will do what you're trying to achieve.

    Component is rendered only once, with the updated cursor.

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

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