この質問は、例で最もよく説明できます。
;; create a basic om app.
lein new mies-om om-tut
lein cljsbuild auto.
次に、次のコードを貼り付けます (内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"))})
のコードwill-mount
は実際に実行されていprintln
ます。関数をドロップすると、それが表示されます。明確でないのは、レンダリング ループが 1 回だけ呼び出される理由です。一方、をブロックom/update!
内にラップすると、期待どおりに機能します。go
;; 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"))})
will-mount
問題は、アプリの状態を更新したため、新しいレンダリング ループがトリガーされないのはなぜですか? 必要なときにブロックを使用go
するのが好きですが、この単純な例をブロックでラップする必要がある理由がわかりません。