2 Replies Latest reply on Oct 29, 2011 6:59 AM by Amy Blankenship

    Eventhandlers of children of application can not receive custom event dispatched by application

    M!scha

      Hello dear Adobe community,

       

      hope you can help me with this issue.

      When the application dispatches a custom event, the child uicomponent can only receive the event by using Application.application.addEventListener or systemManager.addEventListener. Simply adding a listener, without application or systemmanager, does not allow the event to be received by the child component.
      I want to be able to use the addEventListener and removeEventListener without SystemManager or Application.application, or could you provide a better workaround? How can I use the addEventListener, do I need to overwrite EventDispatcher, which I wouldnt like to do?

      Just to clarifiy, if i remove the systemManager in front of addEventListener(CustomEventOne.EventOne,sysManHandleCE,false) it will not add it and will not fire.

       

      The code below is an example for this problem that the event is not getting fired in the right moment. The mainapplication got only a button with the customEventOne that gets dispatched.

       public
       class MyCanvas extends Canvas{
      public function MyCanvas()
      
      
      
      
      
      {
      super();
      
      
      
      
      width=300;
      height=300;
      addEventListener(FlexEvent.CREATION_COMPLETE,handleCC,false,0,true);
      
      
      
      
      }
      
      private function handleCC(event:FlexEvent):void
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      {
      removeEventListener(FlexEvent.CREATION_COMPLETE,handleCC);
      addEventListener(CustomEventOne.EventOne,handleCE,false,0,true); 
      
      
      
      
      addEventListener(Event.REMOVED_FROM_STAGE,handleEvt,false,0,true);
      
      
      
      
       systemManager.addeventListener(CustomEventOne.eventOne,sysManHandleCE,false,0,true);
      
      private function handleEvt(event:Event):void
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      }{ 
      trace("In removed from stage handler");
      
      
      
      
      systemManager.removeEventListener(CustomEventOne.EventOne,sysManHandleCE);
      trace(hasEventListener(FlexEvent.CREATION_COMPLETE));
      
      
      
      
      trace(hasEventListener(CustomEventOne.EventOne));
      
      
      
      
      trace(hasEventListener(Event.REMOVED_FROM_STAGE));
      
      
      
      
      
      }
      private function handleCE(event:CustomEventOne):void
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      {
      trace("I got it");
      
      
      
      
      }
      private function sysManHandleCE(event:CustomEventOne):void
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      {
      trace("I got it");
      
      
      
      
      }
      }
      
      
      

       

       

      public class CustomEventOne extends Event
      {
      
      
      public static const EventOne:String = "EventOne";
      
      
      
      
      
      public function CustomEventOne(type:String, bubbles:Boolean=false, cancelable:Boolean=false)
      
      
      
      
      
      {
      super(type, bubbles, cancelable);
      
      
      
      
      
      }
      
      override public functionclone():Event
      
      
      
      
      
      {
      return newCustomEventOne(type,bubbles,cancelable);
      
      
      
      
      
      }
      }
      
      
      

       

      Thank you in advance,
      Michael

        • 1. Re: Eventhandlers of children of application can not receive custom event dispatched by application
          pauland Level 4

          I think you need to look at event propogation. The object that dispatches an event will be sitting on the display tree. The event propagates up the tree to the roots. Your canvas should be attached to the application, but even then it sits lower in the tree branches than the event dispatcher, so it won't see the event being dispatched because the event is not propagated to the children of the object that dispatches it but to the parent of the object that dispatches it.

           

          So, your canvas is a child of the application, but dispatching the event from the application means that the canvas doesn't see it because events are notified up the tree using the parent link, not the child links.

           

          You may wish to investigate how the display list and event propagation works and then the MVC pattern.

           

          Paul

          • 2. Re: Eventhandlers of children of application can not receive custom event dispatched by application
            Amy Blankenship Level 4

            The Display List doesn't allow this to happen, and it's a good thing it doesn't.  It provides at least a little guidance to newer developers that child components should not concern themselves with what's going on higher up on the display list.  Trying to do so results in an inflexible, tightly coupled application.

             

            Instead, you can do one of several things:

             

            1. Your child components can supply properties and/or methods that the parent can call based on whatever happened.
            2. You can use a Model that the parent changes and the child is bound to.
            3. You can provide a separate EventDispatcher to all components that they use for communication (known as an event bus).