我目前正在尝试学习om.next。
这是我拥有的代码:
(ns hlearn.core
(:require [goog.dom :as gdom]
[om.next :as om :refer-macros [defui]]
[om.dom :as dom]
[sablono.core :as html :refer-macros [html]]))
(enable-console-print!)
(def app-data
(atom {:user ""
:main-menu {:selected :home}}))
;; -------------------------------------------------------------------------
;; Parsing
(defmulti read om/dispatch)
(defmethod read :selected
[{:keys [state]} _ _]
{:value (get-in @state [:main-menu :selected])})
;; -------------------------------------------------------------------------
;; Components
(defui MainMenu
static om/IQuery
(query [this]
[:selected])
Object
(render [this]
(let [{:keys [selected]} (om/props this)]
(println (= selected :home)))))
(def main-menu (om/factory MainMenu))
(defui RootView
Object
(render [this]
(println "Render RootView")
(main-menu)))
(def reconciler
(om/reconciler
{:state app-data
:parser (om/parser {:read read})}))
(om/add-root! reconciler
RootView (gdom/getElement "app"))
我的目标是组件MainMenu
必须true
在控制台上写入(当前写入false
)。
由于读取函数应该返回{:value :home}
(应用状态的值),因此(= selected :home)
应该为真。
在实践中,MainMenu
写false
在控制台上,因为selected
有值nil
。