While I can appreciate the core.async approach mentioned here, is it considered
either bad practice or impossible to simply pass owner from a parent to a child
component, perhaps the child stores owner in its own state and uses set-state!
on this parent owner rather than its own owner?
The advantage would be that for simple communication between a parent and
child, you don't mess with the core.async "infrastructure" which involves the
setup, putting, taking, and properly closing of channels, which, while useful
perhaps for larger mechanisms, seems like overkill for quite simple needs.
On Wednesday, May 28, 2014 2:55:53 AM UTC+2, Daniel Kersten wrote:
> I use core.async for this - pass a channel to the child, have the child put
> messages on it when events occur and have the parent modify its own state in
> response to child events.
>
>
> Something like this would work:
>
>
>
>
>
> (defn child [props owner {:keys [ch]}]
>
>
> (reify
> om/IInitState
>
>
> (init-state [_]
>
>
> {:text ""})
>
>
> om/IRenderState
> (render-state [_ {:keys [text editable?]}]
>
>
> (dom/div nil
>
>
> (if editable?
> (dom/input ...)
>
>
> (dom/div nil text))
>
>
> (dom/button #js {:onClick #(async/put! ch :no-edit)}
>
>
> "No Edit")))))
>
>
>
> (defn parent [props owner opts]
>
>
> (reify
> om/IInitState
>
>
> (init-state [_]
>
>
> {:ch (async/chan)
>
>
> :editable? true})
>
>
> om/IWillMount
> (will-mount [_]
>
>
> (async/go-loop []
>
>
> (when-let [value (async/<! (om/get-state owner :ch))]
>
>
> (condp = value
>
>
> :no-edit (om/set-state! owner :editable? false))
>
>
> (recur))))
>
>
> om/IRenderState
> (render-state [_ {:keys [editable? ch]}]
>
>
> (om/build child props {:state {:editable? editable?}
>
>
> :opts ch}))))
>
>
>
>
> In my own code, I always put [topic value] on my channel and have one channel
> shared between all of my components and components can subscribe to various
> topics using async/sub.
>
>
>
>
>
>
>
> On 28 May 2014 01:14, Jamie Orchard-Hays <[email protected]> wrote:
>
>
> I have a component that uses om/build to render an editable section. The
> parent has :editable? local state. Is there a way for the child to set the
> true/false value of this on the parent? So far haven't discovered how this
> might be done.
>
>
>
>
>
> Cheers,
>
>
>
> Jamie
>
>
>
> --
>
> Note that posts from new members are moderated - please be patient with your
> first post.
>
> ---
>
> You received this message because you are subscribed to the Google Groups
> "ClojureScript" group.
>
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
>
> To post to this group, send email to [email protected].
>
> Visit this group at http://groups.google.com/group/clojurescript.
--
Note that posts from new members are moderated - please be patient with your
first post.
---
You received this message because you are subscribed to the Google Groups
"ClojureScript" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/clojurescript.