Skip to content

Instantly share code, notes, and snippets.

@acron0

acron0/foo.clj Secret

Last active February 22, 2016 17:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save acron0/66659295433a93819719 to your computer and use it in GitHub Desktop.
Save acron0/66659295433a93819719 to your computer and use it in GitHub Desktop.
(def conn (d/create-conn {}))
(d/transact! conn
[{:db/id -1
:app/counter? true
:app/count-main 3
:app/count-sub 6}])
(defmulti read om/dispatch)
(defmethod read :app/counter
[{:keys [state query]} _ _]
(println "Fetching app/counter..." state query)
{:value (d/q '[:find [(pull ?e ?selector) ...]
:in $ ?selector
:where [?e :app/counter?]]
(d/db state) query)})
(defmulti mutate om/dispatch)
(defui Sub
static om/IQuery
(query [this]
[{:app/counter [:db/id :app/count-sub]}])
Object
(render [this]
(println "Sub props:" (om/props this))
(let [{:keys [app/title app/count-sub] :as entity}
(get-in (om/props this) [:app/counter 0])]
(dom/p nil (str "Sub Count: " count-sub)))))
(def other-ui (om/factory Sub))
(def reconciler (om/reconciler
{:state conn
:parser (om/parser {:read read :mutate mutate})}))
(defui Main
static om/IQuery
(query [this]
[{:app/counter [:db/id :app/count-main]}])
Object
(render [this]
(println "Main props:" (om/props this))
(dom/div nil
(let [{:keys [app/title app/count-main] :as entity}
(get-in (om/props this) [:app/counter 0])]
(dom/p nil (str "Main Count: " count-main)))
(other-ui))))
(if-let [node (gdom/getElement "app")]
(om/add-root! reconciler Main node))
@acron0
Copy link
Author

acron0 commented Feb 22, 2016

Main Count: 3

Sub Count:

@acron0
Copy link
Author

acron0 commented Feb 22, 2016

Fetching app/counter... #object [cljs.core.Atom {:val #datascript/DB {:schema {}, :datoms [[1 :app/count-main 3 536870913] [1 :app/count-sub 6 536870913] [1 :app/counter? true 536870913]]}}] [:db/id :app/count-main]
Main props: {:app/counter [{:db/id 1, :app/count-main 3}]}
Sub props: nil
Fetching app/counter... #object [cljs.core.Atom {:val #datascript/DB {:schema {}, :datoms [[1 :app/count-main 3 536870913] [1 :app/count-sub 6 536870913] [1 :app/counter? true 536870913]]}}] [:db/id :app/count-main]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment