Reputation: 443
I'm new to Flex.My question is how to dispatch event between components?As far as I know,Flex only bubbles event to itself or its parent.
I'm in such a situation:
<mx:Application>
<com:Component1 id="comp1" />
<com:Component2 id="comp2" />
</mx:Application>
In Component2.mxml I have two other components called A and B
I want comp1 to dispatch an Event with parameters,according to the parameters some changes could be made to A and B in comp2.How could I get the event in comp2?I know Cairngorm could make a difference,but I don't want to use it right now.Could anyone give me a hand?Much Thanks!
Best,Shuo
Upvotes: 1
Views: 7885
Reputation: 5342
<mx:Application>
<com:Component1 id="comp1" click="comp2.handleComp1Click()"/>
<com:Component2 id="comp2" click="comp1.handleComp2Click()"/>
</mx:Application>
That's very crude, but that's how you can do it. For more complicated cases, write a function (or functions) at the Application level, have those handle the events, and then propagate what you need down to the children. In your example you need to do something at the Application level.
Yes, a proper architecture is probably better.
Upvotes: 0
Reputation: 6565
If Application fires a bubbling event it will be delivered through the entire hierarchy, comp1 and comp2 as well as their respective children. Comp1 should carry the metadata:
[Event(name="someEvent", type="flash.events.Event")]
Now when comp1 dispatches the event, it will be handled in the parent like:
<com:Component1 id="comp1" someEvent="this.dispatchEvent(event,true)"/>
like a relay.
I think it is better to handle this with a proper MVC structure such as Robotlegs, mate, swiz, PureMVC, etc.
Upvotes: 3