I'm working on a web app in Google closure where the structure is something like this:
App
+ Control Pane
| + Search Box
| + Search Button
+ Result Pane
+ Results
+ Next Page Link
The actual component structure is quite a bit more complex. The important point is that there are many different components all over the component tree that can initiate certain actions. In this example, hitting enter in Search Box
, pressing Search Button
, or hitting Next Page
all require a query to be made.
This is simple enough to handle. Any child anywhere in the component tree can do
this.dispatchEvent(App.EventType.ACTION, ...)
App
will be able to listen to it when the event propagates upwards. The problem is the other direction: When App
receives data from its query, it must push it to all children.
It seems very brittle for App
to try to push directly to Search box
and Results
, as their location in the component tree is subject to change. What I'd like to do is fire an App.EventType.DATA_RECEIVED
event and have all children (and sub-children, etc.) hear it.
The only way to do this in google closure that I've been able to find is to make a global public singleton instance of App
and use as the source for all App.EventType.DATA_RECEIVED
events, or to plumb App
through to all children and subchildren.
Both of these are messy and brittle in their own way.
Is there a simple way in closure to dispatch events that bubble downwards?