Last active
January 9, 2021 16:09
-
-
Save swannodette/11308901 to your computer and use it in GitHub Desktop.
Om + DataScript
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-data.core | |
(:require [om.core :as om :include-macros true] | |
[om.dom :as dom :include-macros true] | |
[datascript :as d])) | |
(enable-console-print!) | |
(def schema {}) | |
(def conn (d/create-conn schema)) | |
;; prevent cursor-ification | |
(extend-type d/DB | |
om/IToCursor | |
(-to-cursor | |
([this _] this) | |
([this _ _] this))) | |
(d/transact! conn | |
[{:db/id -1 | |
:title "Hello world!" | |
:count 0}]) | |
(defn get-conn [owner] | |
(om/get-shared owner :conn)) | |
(defn app-title [db] | |
(ffirst | |
(d/q | |
'[:find ?title | |
:where | |
[?e :title ?title]] | |
db))) | |
(defn app-count [db] | |
(ffirst | |
(d/q | |
'[:find ?count | |
:where | |
[?e :count ?count]] | |
db))) | |
(defn increment [e owner count] | |
(d/transact! (get-conn owner) | |
[[:db/add 1 :count (inc count)]])) | |
(defn app-view [db owner] | |
(reify | |
om/IRender | |
(render [_] | |
(let [count (app-count db)] | |
(dom/div nil | |
(dom/h2 nil (app-title db)) | |
(dom/div nil | |
(dom/span nil count) | |
(dom/button | |
#js {:style #js {:marginLeft "5px"} | |
:onClick #(increment % owner count)} | |
"+"))))))) | |
(om/root app-view conn | |
{:shared {:conn conn} | |
:target (. js/document (getElementById "app"))}) |
@afhammad By passing the db to om/root
I believe the entire component tree is re-rendered on every change made to the db.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I understand that this is a proof of concept, but presumably you would pass the db ref as a whole all the way down so that it can be queried. If so what are the implications (if any) on Om's functionality by not cursor-ifying db?