3

この質問は、例で最もよく説明できます。

;; 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するのが好きですが、この単純な例をブロックでラップする必要がある理由がわかりません。

4

1 に答える 1

1

will-mount はカーソルを更新するのに適していないと思います。:fn オプションを指定して om/build を呼び出すと、達成しようとしていることが実行されます。

コンポーネントは、更新されたカーソルで一度だけレンダリングされます。

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

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

于 2015-01-22T22:31:49.410 に答える