Saurabh Gokhale
Saurabh Gokhale

Reputation: 46425

Flex Event Handling

Folks,

I had a doubt in Flex's event handling and dispatching mechanism.

Consider the below diagram,

Both Panels i.e Panel1 and Panel2 are under the same application.

enter image description here

I want to dispatch an event on click on Send Button, and be able to listen for that event by adding event listener on Receive Button.

How can I do that ?

PS : Since, AFAIK, dispatchEvent only dispatches event up the Parent Hierarchy. Is that right ?

Thanks.

Upvotes: 0

Views: 575

Answers (4)

turbosqel
turbosqel

Reputation: 1542

Easiest but bad practise is parentApplication.addEventListener and parentApplication.dispatchEvent . For this You should use global static dispatcher class .

Upvotes: 0

alxx
alxx

Reputation: 9897

adding event listener on Receive Button

There's something wrong here. Add event listener in button code? Is it custom button? Even if so, button should not process events - it should be a simple building block dispatching its own click events and not concerning with anything else.
What you really seem to need is notify one component of another's activity. You can just "use Parsley" like kyohiro suggests, or make your own connector, like this:

public class MessageBus extends EventDispatcher {
    //singleton facility
    private static _instance:MessageBus;

    public static get instance():MessageBus {
        if (!_instance) _instance = new MessageBus();
        return _instance;
    }
}

Then you use this MessageBus to add event listener to it (Panel2) and dispatch events through it (Panel1). This way Panel1 doesn't know about Panel2, they only know both MessageBus. So, to connect two components, you only need unique String constant for each event.

Upvotes: 0

pho
pho

Reputation: 25490

You can't. Events originate from a component and bubble upwards (child to parent), never downwards (parent to child) or sideways (sibling to sibling).

You can instead have an event listener in the parent application, which will invoke some function in panel 2 when it receives the event from panel 1

Upvotes: 0

kyohiro
kyohiro

Reputation: 322

No, you can't. But if you really want to handle events cross views or components without adding too many dependencies, I suggest you take advantage of frameworks like Parsley and their messaging features.

http://www.spicefactory.org/parsley/

Upvotes: 2

Related Questions