3 Replies Latest reply on Feb 10, 2011 4:00 AM by Pablo Souza

    Question on custom events

    CloudNine99

      Hi guys,

                Just want to ask a question about custom bindable events in flex, please consider the following example :

       

      <?xml version="1.0" encoding="utf-8"?>
      <s:Application
      
       xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="
      
      library://ns.adobe.com/flex/spark" xmlns:mx="
      
      library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" creationComplete="initApp()">
      
      
      <fx:Script>
           <![CDATA[
      
           import mx.controls.Alert;
      
           [
      
      Bindable(events="countChanged")]
      
           public var myDispatcher:Dispatcher = new Dispatcher();
      
      
      
           private function initApp():void{
      
                this.addEventListener("countChanged", incrementHandler);     }
      
      
      
      
           public function incrementCount():void{          myDispatcher.count++;
           }
      
      
      
      
           public function incrementHandler(evt:Event = null):void{          Alert.show(
      
      "incrementHandler called!");     }
      
      
           ]]>
      
      
      </fx:Script>
      
      
           <s:HGroup> 
      
                <s:Button click="incrementCount()"/>
      
                <s:Label text="{myDispatcher.count}"/>
      
           </s:HGroup>
      
      </s:Application>
      
      
      
      
      
      
      


      with the follow class:

       

       

      package
      {
      
      import flash.events.Event;
      
      import flash.events.EventDispatcher;
      
      
      
      public class Dispatcher extends EventDispatcher{ 
      
      
           private var _count:int = 0;
      
           [
      
      Bindable(event="countChanged")]
      
           public function get count():int{
      
                return _count;     }
      
      
      
      
           public function set count(value:int):void{          _count = value;
      
      
                var eventObj:Event = new Event("countChanged",true);           dispatchEvent(eventObj);
           }
      
      }
      }
      
      
      
      

       

      My question is, how comes the label shows the correct value for count but the incrementHandler is never called? I thought this setup would only respond to the countChanged event. The reason i am trying this is because i wish to implement custom events in my modelLocator to avoid the overhead of using the propertyChangeEvent.

       

      Thanks for your help.

        • 1. Re: Question on custom events
          Pablo Souza

          Hi,

           

          It will work if you do this:

           

          myDispatcher.addEventListener("countChanged", incrementHandler);


          You are setting the application as the listener for this "countChanged" event.

          If you wanna listen the event inside the application you need creating a child component that dispatches this event.

           

           

          Greets,

          Pablo Souza

          • 2. Re: Question on custom events
            CloudNine99 Level 1

            Thanks for your answer Pablo, when you say 'child component' are you refering to a child that extends UIComponent? Like a panel for example?

             

            Thanks

            • 3. Re: Question on custom events
              Pablo Souza Level 3

              I mean there are a lot of ways to dispatch events.

               

              You can make a custom button that dispatches that event:

               

              [Event(name="countChanged", type="flash.events.Event")]
              public class MyButton extends Button
              {
                  ...
              }

               

              So you can listen the button event just like this:

               

              myButton.addEventListener("countChanged", doSomething);

               

              Or you can do this in MXML in order to listen the same event:

               

              <mx:Button id="myButton" countChanged="doSomething()" />

               

               

               

              Greets,

              Pablo Souza