Created
January 6, 2014 21:43
-
-
Save asolove/8290306 to your computer and use it in GitHub Desktop.
Om polymorphic cursor
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
(def map-state { :layers [{:selected true :title "Layer One"}, {:selected false :title "Layer Two" }]}) | |
; I want a way to write reusable components that take and update data from a cursor, | |
; where the parent decides how the data is calculated and updated. | |
(defn checkbox [checked] | |
(om/component | |
(dom/input {:type "checkbox" :checked checked | |
:onClick #(om/update! checked (not checked)}))) | |
(defn layers [layers] | |
(om/build checkbox | |
(cursor/decorate | |
layers | |
(every? :selected layers) | |
(fn [value] | |
(map layers #(assoc % :selected value)))))) | |
; (cursor/decorate cursor value update) takes a parent cursor, and creates | |
; a new cursor with the given value. When update is called on the new cursor, | |
; it transacts against the parent using the provided function |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
That gives the component a cursor with all the data, rather than just the true/false that a reusable checkbox component would expect. The problem I'm thinking of is: what happens when you have two components that want to display the same data, but expect it in different shapes? It's not a big deal as long as you're writing custom app-specific components, but as soon as you have a reusable component library for things like form controls, autocomplete, validations, etc., we're going to run into this problem. The idea of having truly polymorphic cursors (completely independent of key-paths) is that a parent component can pass data in any shape to a child component, and provide a way for updates in the child to then be molded back into the shape the parent has. To make that happens, they need to be chainable: a zipper cursor based on the data output from a transformation cursor based on a zipper into the global state, etc.