Last active
October 25, 2015 08:07
-
-
Save ThomasDeutsch/ec80942bca3270013032 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
:item-one/title "ITEM 1"} | |
{:db/id -2 | |
:item-two/title "ITEM 2"} | |
{:db/id 0 | |
:selected -2}]) | |
(defmulti read om/dispatch) | |
(defmethod read :selected-item [{:keys [state selector]} _ _] | |
(let [x (map (fn [[k v]] | |
{k (->> (d/pull @state [{:selected v}] 0) | |
(:selected)) }) selector)] | |
{:value (into {} x)})) | |
(defmulti mutate om/dispatch) | |
(defmethod mutate 'app/item-switch [{:keys [state]} _ {new-id :prop}] | |
{:value [:selected-item] | |
:action (fn [] (d/transact! state [[:db/add 0 :selected new-id]]))}) | |
(defui ItemOne | |
static om/IQuery | |
(query [this] | |
[: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] | |
[: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] | |
[{:selected-item {:one (om/get-query ItemOne) | |
:two (om/get-query ItemTwo)}}]) | |
Object | |
(render [this] | |
(println "render root") | |
(let [{:keys [one two]} (-> this om/props :selected-item)] | |
(dom/div nil | |
(if (some? one) (item-one one)) | |
(if (some? two) (item-two two)))))) | |
(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