3

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.

shaft
  • 2,147
  • 2
  • 22
  • 38
  • 1
    Did also notice that and it seems somehow related to the version of om you're using. `[om "0.8.0-beta3"]` and transact! cursor inside will-mount I get the old value. however it's not the case if I switch to `[om "0.7.3"]` The very same code after `lein cljsbuild clean`, will render the new value. – cedric Dec 13 '14 at 00:03
  • 1
    This question is discussed on github https://github.com/omcljs/om/issues/336 – cedric Apr 08 '15 at 21:00

1 Answers1

1

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

cedric
  • 111
  • 4