7 Replies Latest reply on Aug 25, 2010 11:41 PM by jd_ice

    Dispatch a custom event

    jd_ice Level 1

      Hi,

       

         Please anyone help me get through this

      I have this function


      public function init():void

      {

           dispatchEvent(new CustomEvent(CustomEvent.LOAD_DATA));

      }

       

      when called in an object creationComplete, the init() method will not fire,

      but when called in button click event, it does.

       

      the init() function is used to display items on the MX datagrid component which is a custom component,

      the one calling the the custom component is another custom component VBOX, the code for the init() is inside the VBOX custom component.

       

      I'm using Robotlegs and Flex 4.

       

      What could have been wrong?

        • 1. Re: Dispatch a custom event
          Flex harUI Adobe Employee

          You sure it didn't fire?  A much more common error is that the listener

          isn't listening yet.

          • 2. Re: Dispatch a custom event
            jd_ice Level 1

            This isn't fire when called on creationComplete, but when called on click event of a button object it did fire something up and return the expected output. I don't seem to get the logic why it fires on a button event and not on creationComplete event.

            • 3. Re: Dispatch a custom event
              flex4 Level 2

              Hi

               

                       If u don't mind can u post the sample code.. define listener to listen this event...  i think this will not solve ur problem please post the sample code if u don't mind

              • 4. Re: Dispatch a custom event
                Flex harUI Adobe Employee

                If some other thing adds the listener after the creationComplete, then it

                will be there for a button click.  Try a test using hasEventListener before

                dispatching to see if anybody is listening.

                • 5. Re: Dispatch a custom event
                  jd_ice Level 1

                  I'm using RobotLegs, the idea is that, when the above function is called something like this

                   

                   

                  <?xml version="1.0" encoding="utf-8"?>

                  <mx:VBox xmlns:fx="http://ns.adobe.com/mxml/2009"

                  xmlns:s="library://ns.adobe.com/flex/spark"

                  xmlns:mx="library://ns.adobe.com/flex/mx" width="552" height="300" xmlns:views="com.jd.test.views.*"

                  creationComplete="vbox1_creationCompleteHandler(event)" >

                  <fx:Script>

                  <![CDATA[

                  import com.jd.test.events.UserEvent;

                   

                  import mx.events.FlexEvent;

                   

                  private function init():void

                  {

                  dispatchEvent(new UserEvent(UserEvent.LOAD_DATA));

                  }

                  protected function button1_clickHandler(event:MouseEvent):void

                  {

                       init(); <-- this will fire an output

                  }



                  protected function vbox1_creationCompleteHandler(event:FlexEvent):void

                  {

                       init(); <-- this does not

                  }

                   

                  ]]>

                  </fx:Script>

                  <fx:Declarations>

                  <!-- Place non-visual elements (e.g., services, value objects) here -->

                  </fx:Declarations>

                  <views:UserDataDisplay id="userGrid"/>  <--- this is a datagrid custom component

                  <s:Button label="Button" click="button1_clickHandler(event)"/>

                  </mx:VBox>

                  My question is, why did it fires on a button click, and not on object creationComplete? I don't think it something on the listener cause it doesn't throw any error. If the listener is null, I'm certain it will throw error saying "Cannot access a property or method of a null object reference"
                  • 6. Re: Dispatch a custom event
                    BhaskerChari Level 4

                    Hi jd_ice,

                     

                    Where you are trying to listen the UserEvent.LOAD_DATA event ....??? Are you listening the event in UserDataDisplay component..??

                     

                    Can you tell where exactly you are listening to this event and how..??

                     

                     

                    Thanks,

                    Bhasker

                    • 7. Re: Dispatch a custom event
                      jd_ice Level 1

                      I have a Mediator class that has these functions

                       

                       

                       

                       

                      override public function onRegister():void

                      {

                           eventMap.mapListener(view,UserEvent.LOAD_DATA, retrieveHandler);

                      }

                       

                      public function retrieveHandler(event:UserEvent):void

                      {

                           service.getUsersData();

                      }

                       

                       

                       

                       

                      where view refers to the UserDataDisplay

                       

                       

                      Best Regards!