Skip to content

Instantly share code, notes, and snippets.

@Jannis
Created December 7, 2015 21:16
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 Jannis/9d2d2721ae3c02556ac1 to your computer and use it in GitHub Desktop.
Save Jannis/9d2d2721ae3c02556ac1 to your computer and use it in GitHub Desktop.
(ns om-tutorial.core
(:require [goog.dom :as gdom]
[om.next :as om :refer-macros [defui]]
[om.dom :as dom]))
(enable-console-print!)
;;;; Initial (denormalized) data
(def initial-state
{:nodes [{:name "r/ui"
:title "UI Requirements"
:children [{:name "r/ui/red-button"}
{:name "r/ui/green-button"}]
:parent nil}
{:name "r/ui/red-button"
:title "There is a red button"
:children [{:name "r/ui/red-button/big"}
{:name "r/ui/red-button/pressable"}]
:parent {:name "r/ui"}}
{:name "r/ui/red-button/big"
:title "The red button is big"
:children []
:parent {:name "r/ui/red-button"}}
{:name "r/ui/red-button/pressable"
:title "The red button can be pressed"
:children []
:parent {:name "r/ui/red-button"}}
{:name "r/ui/green-button"
:title "There is a green button"
:children []
:parent {:name "r/ui"}}]})
;;;; Reconciler and parser
(defmulti read om/dispatch)
(defmethod read :nodes
[{:keys [query state]} key _]
(let [st @state
result (om/db->tree query (get st key) st)]
{:value result}))
(defmulti mutate om/dispatch)
(def parser
(om/parser {:read read :mutate mutate}))
(def reconciler
(om/reconciler {:state initial-state
:parser parser}))
;;;; UI
(defui Parent
static om/Ident
(ident [this props]
[:node (:name props)])
static om/IQuery
(query [this]
[:name :title]))
(defui Node
static om/Ident
(ident [this props]
[:node (:name props)])
static om/IQuery
(query [this]
[:name :title {:children '...} {:parent (om/get-query Parent)}]))
(defui Root
static om/IQuery
(query [this]
[{:nodes (om/get-query Node)}]))
(om/add-root! reconciler
Root
(gdom/getElement "app"))
@Jannis
Copy link
Author

Jannis commented Dec 7, 2015

Run this using figwheel, then enter the following in a browser REPL:

(om.next/db->tree [:name :title {:children '...} {:parent [:name]}] (get @reconciler :nodes) @reconciler)

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