3 Replies Latest reply on Feb 10, 2007 2:42 PM by Robert S

    EventDispatcher on Custom Class

    Robert S Level 1
      I have been trying to create an event dispatcher in a custom data transfer object class. It's a simple class and I don't know how to make it dispatch an event. I tried extending the EventDispatcher class but that doesn't appear to work either. Any help would be greatly appreciated.
      I have attached the code for the application and the custom class. This should work from what I have read, but I can not get the application to catch the event.
      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" creationComplete="initThis()" layout="absolute">
      import mx.controls.Alert;

      private var _tester:TestClass = new TestClass();

      private function initThis():void{
      addEventListener(TestClass.TEST_ACTION, onTestHandler);
      private function onTestHandler(event:Event):void{
      Alert.show("Event Dispatched");
      <mx:Button x="312" y="150" label="Button" click="{_tester.testEvents()}"/>

      import flash.events.EventDispatcher;
      import flash.events.Event;

      public class TestClass extends EventDispatcher
      public static const TEST_ACTION:String = "test";

      public function testEvents():void{
      dispatchEvent(new Event(TestClass.TEST_ACTION));
        • 1. Re: EventDispatcher on Custom Class
          The problem is in your mxml code <mx:Button x="312" y="150" label="Button" click="{_tester.testEvents()}"/>
          you should omit parenthesis for event handler like this click="_tester.testEvents();

          I hope It does solve your problem
          • 2. Re: EventDispatcher on Custom Class
            Robert S Level 1
            This is not even the issue... the event is parsing just fine... it's not bubbling to the app that created it. You can alway put brackets around the items that function actionscript. This call can be performed either way. You can use the brackets to actually call multiple items on an event handler and seperating them with semi-colons.
            • 3. Re: EventDispatcher on Custom Class
              Robert S Level 1
              I did however find an alternative, though I still don't exactly understand. If I add the event listener directly to the class in the apps creationComplete (initThis()) like:

              _tester.addEventListener(TestClass.TEST_ACTION, onTestHandler);

              The app catches the event, though it's not really the app, now is it? It's the actual class, right? I am confused about this.