这似乎并没有像快速启动教程所说的那样发生:
在Om中,下一个应用程序状态更改由协调器管理。协调器接受新颖性,将其合并到应用程序状态,根据其声明的查询查找所有受影响的组件,并安排重新呈现。
当我更改复选框时,突变函数会更新状态,但的呈现函数永远不会执行。我可以看到REPL中的@app-state状态已经改变了,并且我从未看到来自App的呈现函数的prn的控制台中的输出。这就是我在控制台中看到的所有内容:
[1955.847s] [om.next] transacted '[(om-tutorial.core/switch-topic {:name "b"})], #uuid "c3ba6741-81ea-4cbb-8db1-e86eec26b540"
"read :default" :topics如果我用(swap! app-state update-in [:current-topic] (fn [] "b"))更新REPL中的状态,那么应用程序的呈现函数就会执行。以下是控制台输出:
"read :default" :topics
"read :default" :current-topic
"App om-props " {:topics [{:name "a"} {:name "b"}], :current-topic "b"}
"Topics om-props " {:topics [{:name "a"} {:name "b"}]}以下是完整的代码:
(ns om-tutorial.core
(:require [goog.dom :as gdom]
[om.next :as om :refer-macros [defui]]
[om.dom :as dom]))
(enable-console-print!)
(def app-state (atom {:current-topic "a" :topics [{:name "a"} {:name "b"}]}))
(defmulti read (fn [env key params] key))
(defmethod read :default
[{:keys [state] :as env} key params]
(prn "read :default" key)
(let [st @state]
(if-let [value (st key)]
{:value value}
{:value :not-found})))
(defmulti mutate om/dispatch)
(defmethod mutate 'om-tutorial.core/switch-topic
[{:keys [state]} _ {:keys [name]}]
{:action
(fn []
(swap! state update-in
[:current-topic]
#(identity name)))})
(defui Topics
static om/IQuery
(query [this]
[:topics])
Object
(render [this]
(let [{:keys [topics] :as props} (om/props this)]
(prn "Topics om-props " props)
(apply dom/select #js {:id "topics"
:onChange
(fn [e]
(om/transact! this
`[(switch-topic ~{:name (.. e -target -value)})]))}
(map #(dom/option nil (:name %)) topics)))))
(def topics-view (om/factory Topics))
(defui App
static om/IQuery
(query [this]
'[:topics :current-topic])
Object
(render [this]
(let [{:keys [topics current-topic] :as om-props} (om/props this)]
(prn "App om-props " om-props)
(dom/div nil
(topics-view {:topics topics})
(dom/h3 nil current-topic)))))
(def reconciler
(om/reconciler
{:state app-state
:parser (om/parser {:read read :mutate mutate})}))
(om/add-root! reconciler App (gdom/getElement "app"))下面是project.clj文件:
(defproject om-tutorial "0.1.0-SNAPSHOT"
:description "My first Om program!"
:dependencies [[org.clojure/clojure "1.7.0"]
[org.clojure/clojurescript "1.7.170"]
[org.omcljs/om "1.0.0-alpha24"]
[figwheel-sidecar "0.5.0-SNAPSHOT" :scope "test"]])发布于 2016-08-26 12:21:41
我的应用程序中也有同样的问题,并找到了解决办法(尽管这可能不是最好的解决方案)。可以通过传递父组件的om属性来构造组件。
这样,您的ui应用程序可能会如下所示:
(defui App
Object
(render [this]
(dom/div nil (topics-view (om/props this)))))IQuery绝对是更好的解决方案,但我仍然和您一样有相同的问题。这个解决方案目前在我的项目中有效,我肯定会再看一遍IQuery。
编辑
关于组件、标识和规范化的教程解释了在必要时更新UI必须做些什么。这导致了一种更惯用的解决方案。
发布于 2017-05-28 15:09:02
Om Next出于性能原因不愿触发对查询的重读,以避免不必要地为其调用读取函数,并避免无用的重呈现。要指定查询:current-topic的组件应该重新呈现(以及调用的相关读取函数),您可以在事务向量的末尾提供以下键:
(om/transact! this
`[(switch-topic ~{:name (.. e -target -value)})
:current-topic])参考资料:https://github.com/omcljs/om/wiki/Documentation-(om.next)#transact
https://stackoverflow.com/questions/37212917
复制相似问题