1 Reply Latest reply on Aug 4, 2007 5:57 AM by peterent

    Dispatching Custom Events from a pop-up window

    kbobsdaddy
      My application consists (currently) of three main parts: (1) the main application, (2) several custom components, and (3) a pop-up window..

      I am trying to fire a custom event from the pop-up window which will reset a variable in one of the custom components to null. I've worked through several examples, adapting the techniques to my situation, but nothing so far has worked. My current thought is that the problem may be that the custom components are in a custom namespace.

      Anyone out there have any ideas?
        • 1. Re: Dispatching Custom Events from a pop-up window
          peterent Level 2
          Let's say your pop-up window is in the file com/yourcompany/windows/MyPopUp.mxml (or it could be an ActionScript file, it doesn't matter). You want to dispatch the event in the file com/yourcompany/events/MyEvent.as. This means you have 2 classes, MyPopUp and MyEvent, in two different packages.

          In the MyPopUp class you should declare the event metadata:

          [Event(name="myEvent",type="com.yourcompany.events.MyEvent")]

          Then you can dispatch it whenever you want:

          var e:MyEvent = new MyEvent( "myEvent" );
          // set properties of e
          dispatchEvent( e );

          Now you want some other component to listen for these events. In the file which pops up MyPopUp, you can do this:

          var pop:MyPopUp = PopUpManager.createPopUp( this, MyPopUp, true ) as MyPopUp;
          pop.addEventListener( "myEvent", handleMyEvent);
          PopUpManager.centerPopUp(pop); // optional

          The function handleMyEvent will be called whenever MyPopUp dispatches the custom event. From within the handleMyEvent function you can invoke other functions on other components, passing in the data.

          You can also make another component listener for the event:

          pop.addEventListener("myEvent", otherComponent.handleMyEvent);

          This assumes that otherComponent has a public function called handleMyEvent.