5 Replies Latest reply on Apr 1, 2009 10:19 AM by Newsgroup_User

    Sending events from Class instances

    archemedia Level 4
      Hi,
      I'm having trouble with dispatching events from class instances.
      In a simple MXML app, I added an event listener for a custom event.
      Following the rules of good MVC practices, I have a model class with only one instance (singleton pattern). This instance has no reference to my MXML component. When my model receives data from the server, I want to dispatch an event from this instance (which is not in the displayList!). I could solve this problem with a observer pattern but is there no way to just dispatch the event??
      Help much appreciated!
      Dany
        • 1. Re: Sending events from Class instances
          Cesare Rocchi Level 2
          Yes. There is. Your Model has to extend the EventDispatcher. Like this:

          public class Model extends EventDispatcher { ... }

          then you can have a function like this:

          public function newDataFromServer(...):void {
          this.dispatchEvent( ...);
          }

          As an alternative to extend EventDispatcher, you can implement the IEventDispatcher interface.

          HTH.
          • 2. Re: Sending events from Class instances
            archemedia Level 4
            Hi Cesare,

            I'm afraid this doesn't work. I've included a simplified example.
            Any ideas?

            Dany
            • 3. Sending events from Class instances
              Cesare Rocchi Level 2
              The error is here:

              addEventListener("testEvent", listener);
              myTest = new Test();

              this code attaches an event listener to the Application (the 'this' is implicit), whereas
              you want to listen to notifications from instances of the class Test.
              So the code should be:

              myTest = new Test();
              myTest.addEventListener("testEvent", listener);

              HTH.

              • 4. Re: Sending events from Class instances
                archemedia Level 4
                Hi Cesare,

                It works indeed! Thanks a lot for helping me out here!

                Dany
                • 5. Re: Sending events from Class instances
                  Level 7

                  "archemedia" <webforumsuser@macromedia.com> wrote in message
                  news:gqvt6a$4pi$1@forums.macromedia.com...
                  > Hi Cesare,
                  >
                  > I'm afraid this doesn't work. I've included a simplified example.
                  > Any ideas?
                  >
                  > Dany
                  >
                  > MXML code:
                  >
                  > <?xml version="1.0" encoding="utf-8"?>
                  > <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml"
                  > layout="absolute"
                  > creationComplete="init()">
                  > <mx:Script>
                  > <![CDATA[
                  > import mx.controls.Alert;
                  > private var myTest:Test;
                  >
                  > private function init():void
                  > {
                  > addEventListener("testEvent", listener);
                  > myTest = new Test();
                  > }
                  >
                  > public function listener(event:Event):void
                  > {
                  > Alert.show("Heard ya!");
                  > }
                  > ]]>
                  > </mx:Script>
                  > <mx:Button x="10" y="10" label="Button" click="myTest.doSomething();"/>
                  > </mx:Application>
                  >
                  >
                  > ActionScript Class:
                  >
                  > package
                  > {
                  > import flash.events.Event;
                  > import flash.events.EventDispatcher;
                  >
                  > public class Test extends EventDispatcher
                  > {
                  > public function Test()
                  > {
                  > super();

                  ^^^^^^^^^^^^^^
                  This is your problem. Go back to the definition of EventDispatcher and see
                  what its constructor needs.