Skip to content

Instantly share code, notes, and snippets.

@petterik
Last active October 14, 2015 14:26
Show Gist options
  • Save petterik/8444cd3ec9879c40a88f to your computer and use it in GitHub Desktop.
Save petterik/8444cd3ec9879c40a88f to your computer and use it in GitHub Desktop.
;; Context: I'm using datascript and representing a Component as an entity.
;; Discovered a few problems when trying to nest components.
;; Problem 1:
;; When this EntityComponent is updated, it tries to read :db/id :person/name
;; and :person/likes.
;; Without context (like entity id in this case) those attributes may match many entries.
;;
;; Question:
;; Would it make sense to (assoc env :ident (ident c (props c))) in om/default-ui->props so
;; that (read ..) could get the ident of the component and use it as a look up ref:
(defui EntityComponent
static om/IQuery
(query [this]
[:db/id :person/name :person/likes]) ;; <-- needs entity id when component wants to re-render?
static om/Ident
(ident [this {:keys [person/name]}]
[:person/name name])) ;; <- datascript lookup ref
(defmulti read om/dispatch)
(defmethod read :default
[{:keys [state ident selector]} key _]
(let [sel (if selector {key selector} key)]
{:value (d/pull (d/db state) [sel] ident)})) ;; <- using ident to find the state
;; Problem 2:
;; If a component does reverse lookups, like :person/_likes, the component is not
;; re-rendered if the attribute :person/likes is changed, for example:
[[:db/retract [:person/name "Petter"] :person/likes [:hobby :foosball]
;;
;; Question 2:
;; Would it make sense to let om.next know that :person/likes and :person/_likes
;; are related, and re-render :person/_likes if :person/likes change?
(defui ReverseLookup
static om/IQuery
(query [this]
[:db/id :hobby {:person/_likes (om/get-query EntityComponent)}])
..)
;; Thanks!
;; twitter: @petterik_
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment