2 Replies Latest reply on Jun 18, 2009 7:24 AM by Flex harUI

    where is event object instantiated

    haibin_sh

      Hi,

       

      I'm new to Flex. In the code below, where is "event" object in <mx:Button ... /> instantiated? I find if I change it to other names, it does not work. Is it instantiated in mx:Application?

       

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" backgroundColor="white">
          <mx:Script>
              <![CDATA[
                  import mx.controls.Alert;
                  public function clickHandler(clickEvent:Event):void
                  {
                      Alert.show("Event Type:" + clickEvent.type +
                          " came from:" + clickEvent.currentTarget.id);
                  }
              ]]>
          </mx:Script>
          <mx:Button label="Handle Click" id="myButton" click="clickHandler(event)"/>
      </mx:Application>

       

       

       

       

      Regards,

      Haibin

        • 1. Re: where is event object instantiated
          dzeikei

          click="clickHandler(event)"

          is just an MXML equivalent of:

          myButton.addEventListener(MouseEvent.CLICK, clickHandler);

           

          The event is instantiated inside the myButton and dispatched from it.

          The dispatched event is always called 'event' in an event-listening attribute, so I guess it's just a Flex MXML convention.

          1 person found this helpful
          • 2. Re: where is event object instantiated
            Flex harUI Adobe Employee

            Technically, a wrapper function is generated so the actual code looks more like:

             

            private function myButton_clickHandler(event:MouseEvent):void

            {

                 clickHandler(event);

            }

            myButton.addEventListener("click", myButton_clickHandler);

             

            which is why calling myButton.removeEventListener(clickHandler) has no effect.

             

            Alex Harui

            Flex SDK Developer

            Adobe Systems Inc.

            Blog: http://blogs.adobe.com/aharui

            1 person found this helpful