5 Replies Latest reply on Jun 15, 2010 2:09 PM by msakrejda

    Returning a Value from a DispatchEvent


      I have a custom event class that looks like this:


      package com.ascentify.components.eduspeak.events
           import flash.events.Event;
           public class EduSpeakEvent extends Event
                //Build Path Events
                public static const BUILD_PATH:String = "EduSpeakEvent.BuildPath";
                public var param:Object;
                public function EduSpeakEvent(type:String, pParam:Object=null, bubbles:Boolean=false, cancelable:Boolean=false)
                     param = pParam;
                     super(type, bubbles, cancelable);
                public override function clone():Event
                     return new EduSpeakEvent( type, param );
                public override function toString():String
                     return formatToString( "EduSpeakEvent", "param", "type", "bubbles", "cancelable" );


      This is so I can pass a value with my function.


      In an event dispatcher class, I have a function set up to dispatch this event:


      public function buildPath(relativePath:String):Object{
           return dispatchEvent(new EduSpeakEvent(EduSpeakEvent.BUILD_PATH, {relativePath:relativePath}));


      Therein lines the problem. I can send a value to the event handler, but I need to return a value as well:


      public function buildPath(e:EduSpeakEvent):String
           var path:String = e.param.relativePath;
           try {
                // If input  is an absolute path or URL, return it.
                if (isAbsolutePath(e.param.relativePath)) {
                return e.param.relativePath;
                // get the current location
                var baseDir:String = ExternalInterface.call("ES_getBaseDirectory");
                displayLoader._trace("baseDir = "+baseDir, true, true);
                // currently, always returns a concatenated path.
                if (e.param.relativePath.charAt(0) != "/" && e.param.relativePath.charAt(0) != "\\")
                     e.param.relativePath = "/" + e.param.relativePath;
                path = baseDir + e.param.relativePath;
           catch (e:Error) {
                var errMsg:String = "[Exception occurred during 'buildPath']: " + e.message;
           return path;



      Can I get a return value on my eventdispatch function?

        • 1. Re: Returning a Value from a DispatchEvent
          jsd99 Level 3

          Could you have the receiving object dispatch another event back to the original dispatching object?

          1 person found this helpful
          • 2. Re: Returning a Value from a DispatchEvent
            GordonSmith Level 4

            The event system is not designed for returning values and you really shouldn't be using events to do that. Event are for notification, not for function calls. Events can have multiple listeners, so how could they all return a value? However, if you really want to, your listener could set a field of the event object (say, event.result) which you could then query after dispatchEvent(event) returns.


            BTW, why are you passing your path as an Object rather than as a String or whatever it actually is?


            Gordon Smith

            Adobe Flex SDK Team

            • 3. Re: Returning a Value from a DispatchEvent
              lkenney2 Level 1

              Hi Gordon. I'm using an object because I'm using the same event for multiple events. With an object, I can pass mulipitle values, like the below example.


              public function _trace(txt:String, flashTrace:Boolean = true, recAppDebug:Boolean=false):void{
                   dispatchEvent(new EduSpeakEvent(EduSpeakEvent.TRACE, {txt:txt, flashTrace:flashTrace, recAppDebug:recAppDebug}));


              The events I need to return values only have one event listener.


              I realize using the event system to pass and set values isn't the best way to do it, but my singleton class failed and because of the nature of the project, I can't pass anything directly from one component to the next - I need an intermediary.


              Calling functions usually results in a stack overflow.


              Thanks for the help, I'll try what you suggested.

              • 4. Re: Returning a Value from a DispatchEvent
                lkenney2 Level 1

                Hi Gordon. My brain appears not to be working today. Could you tell me how you would query the result data after the event has been dispatched? Thanks.

                • 5. Re: Returning a Value from a DispatchEvent
                  msakrejda Level 4
                  public function _trace(txt:String, flashTrace:Boolean = true, recAppDebug:Boolean=false):Object {
                  event:EduSpeakEvent = new EduSpeakEvent(EduSpeakEvent.TRACE, {txt:txt, flashTrace:flashTrace, recAppDebug:recAppDebug})
                       return event.param;

                  As Gordon said, though, that's pretty smelly.