Skip to content

Instantly share code, notes, and snippets.

@olivergeorge
Created April 11, 2015 05:08
Show Gist options
  • Save olivergeorge/960dd21e6ec3ffa5232c to your computer and use it in GitHub Desktop.
Save olivergeorge/960dd21e6ec3ffa5232c to your computer and use it in GitHub Desktop.
(defprotocol IClear
(clear! [x]))
(defn field-as-value [field-cursor]
(reify
IDeref
(-deref [_] (:value field-cursor))
ISwap
(-swap! [o f] (reset! o (f @o)))
(-swap! [o f a] (reset! o (f @o a)))
(-swap! [o f a b] (reset! o (f @o a b)))
(-swap! [o f a b xs] (reset! o (apply f @o a b xs)))
IReset
(-reset! [o new-value] (om/update! field-cursor :value new-value))
IClear
(clear! [_] (om/transact! field-cursor :value (:initial field-cursor)))))
(defn state-as-value [owner key]
(reify
IDeref
(-deref [_] (om/get-state owner key))
ISwap
(-swap! [o f] (reset! o (f @o)))
(-swap! [o f a] (reset! o (f @o a)))
(-swap! [o f a b] (reset! o (f @o a b)))
(-swap! [o f a b xs] (reset! o (apply f @o a b xs)))
IReset
(-reset! [o new-value] (om/set-state! owner key new-value))
IClear
(clear! [_] (om/set-state! owner key nil))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment