That does make things more ignorant which is very nice. Not sure why I
didn't see that originally given I usually jump all over coupling.
Guess I need some more sleep ;)
Thanks Mike.
On 27 March 2015 at 13:43, Jane Dampney <
janed...@gmail.com> wrote:
> None of our reusable components know about re-frame. None of them dispatch directly. That's the job of the application specific code using both re-frame and re-com (our reusable component library).
>
> Instead, our (re-com) reusable components are given (by the app) on-click callbacks, or on-change callbacks, etc. And these callbacks are often called back with an identifier (what got clicked, or what changed).
>
> When the app creates the component, it supplies these callbacks, and organises for the callback to do the dispatch. The app creates the glue between the libraries.
>
> Here's an example (we use named parameters):
>
> [radio-button
> :model some-subscription-val ;; the currently selected value in the button group
> :value "green"
> :label "make it green"
> :on-change (fn [val] (dispatch [:set-colour val])]
>
> Notice that the reusable component “radio-button” doesn't dispatch itself. It just calls back to a passed callback, which the app code creates (and which happens to dispatch). So the app code itself is the glue between the reusable components and re-frame.
>
> In your case, with a tree, I'd imagine something like this sketch:
>
> Your reusable component requires the tree be specified in a certain way. Eg: Each node has an "id", "a label" and, optionally, "children".
>
> (def tree-data
> [ {:id 1 :label "item 1" :children [{:id "me" :label "me"}
> {:id "them" :label "not me"}
> ]}
> {:id 2 :label "item 2" :children [{:id :sub1 :label "sub1"}
> {:id :sub2 :label "sub2"}
> ]}]):
>
>
> Then you'd have a tree component which an app can use like this:
>
> [tree
> :nodes tree-data
> :on-click (fn [id] (dispatch [:node-clicked id]))]
>
> Notice how the component calls back with the :id of the clicked item. (Supplied by you in the tree-data structure).
>
> --
> Mike