Last active
October 25, 2015 11:36
-
-
Save ThomasDeutsch/141ba1d7c8691418869c 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 scheduler.main.core | |
(:require [goog.dom :as gdom] | |
[om.next :as om :refer-macros [defui]] | |
[om.dom :as dom] | |
[datascript.core :as d])) | |
(enable-console-print!) | |
;; TASK: | |
;; render ItemOne <OR> ItemTwo, based on the ":selected" ref of entity 0 | |
(def schema {:selected {:db/valueType :db.type/ref}}) | |
(def conn (d/create-conn schema)) | |
(d/transact! conn | |
[{:db/id -1 | |
:type :one | |
:item-one/title "ITEM 1"} | |
{:db/id -2 | |
:type :two | |
:item-two/title "ITEM 2"} | |
{:db/id 0 | |
:selected -1}]) | |
(defmulti read om/dispatch) | |
(defmethod read :app/item1 [{:keys [state selector]} _ _] | |
{:value (->> (d/pull @state [{:selected selector}] 0) | |
(:selected))}) | |
(defmethod read :app/item2 [{:keys [state selector]} _ _] | |
{:value (->> (d/pull @state [{:selected selector}] 0) | |
(:selected))}) | |
(defmulti mutate om/dispatch) | |
(defmethod mutate 'app/item-switch [{:keys [state]} _ {new-id :prop}] | |
{:value [:app/item1 :app/item2] | |
:action (fn [] (d/transact! state [[:db/add 0 :selected new-id]]))}) | |
(defui ItemOne | |
static om/IQuery | |
(query [this] | |
[:type :item-one/title]) | |
Object | |
(render [this] | |
(println "render ItemOne") | |
(let [{:keys [item-one/title]} (om/props this)] | |
(dom/div nil | |
(dom/h2 nil title) | |
(dom/button | |
#js {:onClick | |
(fn [e] | |
(om/transact! this | |
`[(app/item-switch {:prop 2})]))} | |
"switch to item 2"))))) | |
(def item-one (om/factory ItemOne)) | |
(defui ItemTwo | |
static om/IQuery | |
(query [this] | |
[:type :item-two/title]) | |
Object | |
(render [this] | |
(println "render ItemTwo") | |
(let [{:keys [item-two/title]} (om/props this)] | |
(dom/div nil | |
(dom/h2 nil title) | |
(dom/button | |
#js {:onClick | |
(fn [e] | |
(om/transact! this | |
`[(app/item-switch {:prop 1})]))} | |
"switch to item 1"))))) | |
(def item-two (om/factory ItemTwo)) | |
(defui Root | |
static om/IQuery | |
(query [this] | |
[{:app/item1 (om/get-query ItemOne)} | |
{:app/item2 (om/get-query ItemTwo)}]) | |
Object | |
(render [this] | |
(println "render root") | |
(let [{:keys [app/item1 app/item2]} (-> this om/props)] | |
(println "item1, item2: " item1 item2) | |
(dom/div nil | |
(if (= (:type item1) :one) | |
(item-one item1) | |
(item-two item2)))))) | |
(def reconciler | |
(om/reconciler | |
{:state conn | |
:parser (om/parser {:read read :mutate mutate})})) | |
(om/add-root! reconciler | |
Root (gdom/getElement "app")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment