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

    Question on custom events

    CloudNine99 Level 1

      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"?>
       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()">
           import mx.controls.Alert;
           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!");     }
                <s:Button click="incrementCount()"/>
                <s:Label text="{myDispatcher.count}"/>

      with the follow class:



      import flash.events.Event;
      import flash.events.EventDispatcher;
      public class Dispatcher extends EventDispatcher{ 
           private var _count:int = 0;
           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 Level 3



          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.




          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?



            • 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()" />





              Pablo Souza