Skip to content

Instantly share code, notes, and snippets.

@retro
Last active February 14, 2016 17:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save retro/e0813592749f693f3b2a to your computer and use it in GitHub Desktop.
Save retro/e0813592749f693f3b2a to your computer and use it in GitHub Desktop.
;; If it's for the user stored in the `:current-user` named slot
(def schema {:users {:id :id}})
(def current-user-favorite-color [app-db]
(reaction
(let [db @app-db
current-user (edb/get-named-item schema app-db :users :current)]
(:favorite-color current-user))
(defn favorite-color-renderer [ctx]
(fn []
[:div (deref (ui/subscription ctx :current-user-favorite-color))]))
(def favorite-color-component
(ui/constructor {:renderer favorite-color-renderer
:subscription-deps [:current-user-favorite-color]))
;; If you have only the id
(defn favorite-color-for-user-id [app-db user-id]
(reaction
(let [db @app-db
user (edb/get-item-by-id schema app-db :users user-id]
(:favorite-color user))
(defn favorite-color-by-user-id-renderer [ctx user-id]
(fn []
(let [favorite-color (deref (ui/subscription ctx :favorite-color-for-user-id [user-id]))]
[:div favorite-color]))
(def favorite-color-by-user-id-component
(ui/constructor {:renderer favorite-color-by-user-id-renderer
:subscription-deps [:favorite-color-for-user-id]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment