Kevin
Kevin

Reputation: 23634

Dispatching events from a ActionScript class

package com.services
{

        import com.asfusion.mate.events.ResponseEvent;
        import com.events.navigation.DesgManagementEvent
        import flash.events.EventDispatcher;    
        import mx.controls.Alert;

        public class UserManager extends EventDispatcher
        {

          [Bindable]
            public var addResult:String

               [Bindable]
            public var user:User

            public function UserManager()
            {
            }


            public function addUsersResult(Result:String):void {        
                addResult = Result
                //Alert.show(event.result.toString());
                Alert.show(addResult);
                backHome();
            }

            private function addUsersFault(event:ResponseEvent):void {
                Alert.show(event.faultString, "Error Executing Call");
            }


            private function backHome():void {
                this.dispatchEvent(new DesgManagementEvent(DesgManagementEvent.DES_HOME));

            } 

    }
}

I am able to get the result, but not able to dispatch the event from the custom actionScript class. I googled and got the riposte that you need to add it to display list.

Can anyone figure out where i am going wrong. The method backHome is not being called at all.

Upvotes: 0

Views: 596

Answers (2)

bob
bob

Reputation: 11

Are you sure you're receiving type String when addUsersResult() is called?

Upvotes: 0

Laura
Laura

Reputation: 366

I believe you are expecting to get DesgManagementEvent in the event map and because you don't see it being handled, you believe that bakcHome is not being called.

As you said, events dispatched from an object component that is not in the display list will never reach the event map. You need to pass the dispatcher and use that to dispatch the event. You can pass it in the constructor (first objectBuilder) or as a property (second objectBuilder).

<EventHandlers type="{FlexEvent.INITIALIZE}">
  <ObjectBuilder generator="{MyManager}" constructorArguments="{scope.dispatcher}"/>
  <ObjectBuilder generator="{MyManager2}">
    <Properties dispatcher="{scope.dispatcher}"/>
  </ObjectBuilder>
</EventHandlers>

If you use the constructor, then it will look something like this:

public function MyManager(dispatcher:IEventDispatcher)
{
    this.dispatcher = dispatcher;
}

Then you will use your dispatcher property to dispatch the event:

dispatcher.dispatchEvent(new DesgManagementEvent(DesgManagementEvent.DES_HOME));

Upvotes: 1

Related Questions