I’m new to flex , so forgive me if this is a dumb question.
Right now I’m using custom events to pass data from one component to another. My problem is that events only bubble up. How can I pass data to a component that isn’t a parent of the component dispatching the event?
Here’s the basic layout. I’m trying to get data from component 1 passed to component 3.
Application MXML
Component 1
Component 2
Component 3
If a piece of data is required by all components in a graph/tree, your best bet is to expose a public bindable property on each. Let the child components dispatch a bubbling event that is handled by the parent, who can set the new value of the bindable property. If you bind the property from the parent down to the child this will “cascade” down to the other components.
If you need to invoke additional logic, you can define a get/set pair instead of public var and add logic to the setter:
Even better would be to use Flex’s invalidation framework to optimize performance:
This pattern is used all over the place in all the UIComponents that make up the Flex framework. You may also need to override updateDisplayList(…) to position elements.