Skip to content

Instantly share code, notes, and snippets.

@hlolli
Created February 2, 2017 16:26
Show Gist options
  • Save hlolli/1ed18c0c5481af99cd13da86db16ca11 to your computer and use it in GitHub Desktop.
Save hlolli/1ed18c0c5481af99cd13da86db16ca11 to your computer and use it in GitHub Desktop.
(ns omx
(:require
[goog.dom :as gdom]
[om.next :as om :refer-macros [defui]]
[om.dom :as dom]))
(def init-state {:data-1 {:a 1 :b 2}
:data-2 {:c 3 :d 4}
:data-3 {:e 5 :f 6}})
(defmulti read om/dispatch)
(def p (om/parser {:read read}))
(defmethod read :data-1
[{:keys [state query]} k _]
(println "data-1 called")
{:value (get @state k)})
(defmethod read :data-2
[{:keys [state query]} k _]
(println "data-2 called")
{:value (get @state k)})
(defmethod read :data-3
[{:keys [state query]} k _]
(println "data-3 called")
{:value (get @state k)})
(defmethod read :link-B
[{:keys [state query] :as env} k _]
(let [q (if (vector? query) query [query])]
(println query)
{:value (om/db->tree q (p env q) @state)}))
(defmethod read :link-C
[{:keys [state query] :as env} k _]
(let [q (if (vector? query) query [query])]
{:value (om/db->tree q (p env q) @state)}))
(defui C
static om/IQuery
(query [this]
'[[:data-3 _]])
Object
(render [this]
(dom/p nil (str "C props: " (om/props this)))))
(def c (om/factory C))
(defui B
static om/IQuery
(query [this]
`[{:link-C ~(om/get-query C)}
[:data-2 ~'_]])
Object
(render [this]
(dom/div nil
(dom/p nil (str "B props: " (om/props this)))
(c (:link-C (om/props this))))))
(def b (om/factory B))
(defui A
static om/IQuery
(query [this]
[{:link-B (om/get-query B)}
:data-1])
Object
(render [this]
(dom/div nil
(dom/p nil (str "A props: " (om/props this)))
(b (:link-B (om/props this))))))
(def reconciler
(om/reconciler
{:state init-state
:parser p}))
(om/add-root! reconciler
A js/klipse-container)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment