10 Replies Latest reply on Nov 23, 2011 10:19 PM by drkstr_1

    Is this possible with Actionscript 3 events?

    Rick.Blacker

      Fairly new to AS3. Studying up on events.

       

       

      Is it possible to have a simple value object dispatch an event and have the script inside the MXML respond to that event itself without having to register the event on the value object itself?

       

      Example of what I'm talking about

       

      I have a simple value object named Person. Not directly inherited from anything.

      Has one variable.  firstName : String;

       

      When the firstName variable gets changed, the setter creates a new DispatchEvent object and dispatches a new event. It does this without any runtime errors.  Back in the MXML, I want the MXML's Application to be what registers the event, NOT the Person object. In fact, it can't because I didn't inhert from the DispatchEvent class.

       

      I've provided both the custom class and the MXML. I've kept it as bare bones and as simplistic as possible to get my intent across.

       

       

       

      =======================================

      MY CUSTOM CLASS DEFINITION

      =======================================

       

      package myClasses
      {
      import flash.events.Event;
      import flash.events.EventDispatcher;

      public class Person
      {
        private var _firstName : String;
       
        public function Person( fName : String )
        {
         _firstName = fName;
        }

       

        [Bindable(event="firstNameChange")]
        public function get firstName():String
        {
         return _firstName;
        }

       

        public function set firstName(value:String):void
        {
         if( _firstName !== value)
         {
          _firstName = value;
         
          var d:EventDispatcher = new EventDispatcher( );
          d.dispatchEvent( new Event( "firstNameChange" ) );
         }
        }
      }

      }

       

       

       

      =======================================

      MY MXML

      =======================================

       

      <?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="application1_creationCompleteHandler(event)">

       

      <fx:Script>

        <![CDATA[

         import mx.controls.Alert;

         import mx.events.FlexEvent;

         import myClasses.Person;

        

         [Bindable]

         protected var myPerson:Person

        

         protected function application1_creationCompleteHandler(event:FlexEvent):void

         {

          

           // Want the "application" to be notified when the "firstNameChange" event happens

          addEventListener( "firstNameChange", personChanged );

          myPerson = new Person( "Scoobie Do" );

         

          // Force a change and HOPE the event triggers

          myPerson.firstName = "Shaggy";

         }

        

           // This is NOT being ran.

         protected function personChanged( e:Event ) : void

         {

          Alert.show( "triggered" );

         }

        

        ]]>

      </fx:Script>

      </s:Application>

       

       

       

       

       

       

      All the examples I've seen would have the Person class inherit from the DispatchEvent class, then back in the MXML you'd have something like this

      var p:Person = new Person( "thelma" );

      p.addEventListener( "firstNameChange", someEventHandlerFunction );

       

      I get that, but not what I'm trying to acheive here. I just want the Person object to dispatch the event and have someone else be able to listen for the "firstNameChange" event.   Is this possible?

       

      Thanks for the help!!!!