这个问题最好用一个例子来解释:
;; 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
函数,那么你会看到。不清楚的是为什么渲染循环只被调用一次。另一方面,如果您将 包装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
在需要时使用块,但我不明白为什么我不得不将这个简单的示例包装在一个块中。