apache-flexactionscript-3eventsevent-dispatching

I can't dispatch custom event from one module to another as it gives TypeError: Error #1034: Type Coercion failed:


I am trying to dispatch a custom event from one flex module to another.

The code which dispatch the event is as below

Application.application.Destination.child.dispatchEvent(
    new AlgoEvent(AlgoEvent.GETFROMPARENT_LOCAL_EVENT));

here AlgoEvent is a custom event

on the other side the module which catches and handles the event has this code:

public  function sendParametersToChild(e:AlgoEvent):void
{
    //some codes
}

but when the statement Application.application.Destination.child.dispatchEvent(new AlgoEvent(AlgoEvent.GETFROMPARENT_LOCAL_EVENT)); is executed the debugger give the following run time exception:

TypeError: Error #1034: Type Coercion failed: cannot convert resources.events::AlgoEvent@4182239 to resources.events.AlgoEvent.
    at flash.events::EventDispatcher/dispatchEventFunction()
    at flash.events::EventDispatcher/dispatchEvent()
    at mx.core::UIComponent/dispatchEvent()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\core\UIComponent.as:9298]
    at components::Destination/sendTimeToChild()[E:\FlexProjects\MyApp\src\components\Destination.mxml:99]
    at components::Destination/updateParameters()[E:\FlexProjects\MyApp\src\components\Destination.mxml:206]
    at components::Destination/__CreateBasketButton_click()[E:\FlexProjects\MyApp\src\components\Destination.mxml:558]

I am not able to identify what is going wrong here. Please help to solve this problem

This is my Event class

public class AlgoEvent extends Event
{
    public static const GETFROMPARENT_LOCAL_EVENT:String = "getfromparent_local";
    private var eventType:String;

    public function AlgoEvent(eventType:String, bubbles:Boolean=false, cancelable:Boolean=false)
    {
        super(eventType,bubbles,cancelable);
        this.eventType=eventType;
    }
}

While debugging am getting error in this funcion of UIComponent class

override public function dispatchEvent(event:Event):Boolean
{
    if (dispatchEventHook != null)
        dispatchEventHook(event, this);

    return super.dispatchEvent(event); 
}

Excaxtly this line gives the error: dispatchEventHook(event, this);


Solution

  • Import the AlgoEvent class in the main application and create a reference to it.

    import resources.events.AlgoEvent;
    private var dummyEvent: AlgoEvent;
    

    Some explanations for this could be found here: Module domains

    If your custom event doesn't carry any special event properties you could workaround your problem by using the standard Event class.

    dispatchEvent(new Event(AlgoEvent.GETFROMPARENT_LOCAL_EVENT));