Created
May 15, 2015 00:23
-
-
Save olivergeorge/6b23e75db9b880fbfc3e to your computer and use it in GitHub Desktop.
Refactor of observe and ref-cursor assuming app-state assessable via om/get-shared
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-tick.component | |
" | |
We assume that components have :app-state in shared. | |
" | |
(:require [om.core :as om])) | |
(defn- korks-it [korks] | |
(cond | |
(nil? korks) [] | |
(sequential? korks) korks | |
:else [korks])) | |
(defn root-cursor | |
"Given an om-tick style component, return root-cursor" | |
[c] | |
(assert (om/component? c) c) | |
(om/root-cursor (om/get-shared c :app-state))) | |
(defn ref-cursor | |
([c] | |
(assert (om/component? c) c) | |
(om/ref-cursor (root-cursor c))) | |
([c path] | |
(assert (om/component? c) c) | |
(assert path path) | |
(let [root (root-cursor c) | |
path (korks-it path)] | |
(assert (get-in root path) (str "No value for " (pr-str path) " root-cursor")) | |
(om/ref-cursor (get-in root path))))) | |
(defn observe | |
"Given an om-tick style component, observe." | |
([c] | |
(assert (om/component? c) c) | |
(om/observe c (root-cursor c))) | |
([c path] | |
(println :observe path) | |
(assert (om/component? c) c) | |
(om/observe c (ref-cursor c (korks-it path))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment