Created
December 28, 2015 23:54
-
-
Save awkay/d8a5f5b7e221c61b537c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns om-tutorial.core | |
(:require [goog.dom :as gdom] | |
[om.next :as om :refer-macros [defui]] | |
[cljs.pprint :refer [pprint]] | |
[om.dom :as dom])) | |
(enable-console-print!) | |
(def init-data | |
{:current-user {:email "bob.smith@gmail.com" :things [[:thing/by-id 1] [:thing/by-id 2]]} | |
:thing/by-id { | |
1 {:id 1 :name "Hello"} | |
2 {:id 2 :name "There"} | |
} | |
:items [{:id 0 :title "Foo"} | |
{:id 1 :title "Bar"} | |
{:id 2 :title "Baz"}]}) | |
(defmulti read om/dispatch) | |
(defmethod read :items | |
[{:keys [query state]} k _] | |
(let [st @state] | |
{:value (om/db->tree query (get st k) st)})) | |
(defui Thing | |
static om/Ident | |
(ident [_ {:keys [id]}] | |
[:thing/by-id id]) | |
static om/IQuery | |
(query [_] | |
'[:id :name]) | |
Object | |
(initLocalState [this] {:a true}) | |
(render [this] | |
(let [{:keys [name]} (om/props this)] | |
(dom/div nil | |
(dom/b nil (if (get (om/get-state this) :a) "A" "a")) | |
(dom/button #js {:onClick (fn [] | |
(println "Click") | |
(om/update-state! this update :a not))} name)) | |
))) | |
(def thing (om/factory Thing {:keyfn :id})) | |
(defn user [u] | |
(let [{:keys [email things]} u] | |
(dom/div nil | |
(dom/ol nil (mapv #(thing %) things)) | |
(dom/div nil email)))) | |
(defui Item | |
static om/Ident | |
(ident [_ {:keys [id]}] | |
[:item/by-id id]) | |
static om/IQuery | |
(query [_] | |
[:id :title {[:current-user '_] [:email {:things (om/get-query Thing)}]}]) | |
Object | |
(render [this] | |
(let [{:keys [title current-user]} (om/props this)] | |
(dom/li nil | |
(dom/div nil title) | |
(user current-user) | |
)))) | |
(def item (om/factory Item)) | |
(defui SomeList | |
static om/IQuery | |
(query [_] | |
[{:items (om/get-query Item)}]) | |
Object | |
(render [this] | |
(dom/div nil | |
(dom/ul nil | |
(map item (-> this om/props :items)))))) | |
(def reconciler | |
(om/reconciler | |
{:state init-data | |
:parser (om/parser {:read read})})) | |
(om/add-root! reconciler SomeList (gdom/getElement "app")) | |
(comment | |
(pprint @reconciler)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment