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

StackOverflow https://stackoverflow.com/questions/5401472

Question

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);

Was it helpful?

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));

OTHER TIPS

I had the same problem when dispatching, solved overriding two functions:

override public function clone():Event
{
    return new AlgoEvent(type, bubbles, cancelable);
}

override public function toString():String
{
        return formatToString("AlgoEvent","type"","bubbles","cancelable","eventPhase");
}

hope it helps out :)

Mr. splash suggested a solution which worked fro me:

Try to make the Custum Event (Algo Event in my case) class known to the main application.

I.e import it in the main application and create a variable of it..

And it works for a main reason>>when we try to communicate betwwen the modules using event dispatching what happens is :the modules are loaded at the run time but the classes like event classes are linked to the modules at the run time.. But the Event class is compiled before the modules are loaded..

application defines a Custum Event Class at compile time, and the module defines its own Custum Event Class when it is published. Then when the application is run, the Custum Event Class dispatched in the application doesn't match the one in the module swf.

For the problem which is causing this error one can check the link:

http://www.kirupa.com/forum/showthread.php?t=320390

and also

http://www.jeffdepascale.com/index.php/flash/custom-events-in-loaded-swf-files/

Mate framework takes care of all this. It gives you a global event bus, for all modules in your app. http://mate.asfusion.com/

Try to override the clone() method in your customized Event,AlgoEvent. Add the following code to your AlgoEvent.as class and try:

override public function clone():Event{ return new AlgoEvent(eventType,bubbles,cancelable); } HTH.

Your custom Event class should look like this:

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

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

    override public function clone():AlgoEvent
    {
        return new AlgoEvent(type, bubbles, cancelable);
    };
};

You should use the Event's inherited type property instead of creating a new one.

Also, the UIComponent has it's own dispatchEvent method, so you don't have to create your own - only if it works differently to the inherited one.

Regards, Rob

Okay, it must be said that what you're doing, from an architectural standpoint, is wrong. Calling Application.application is bad for so many reason, especially if you're then starting to go down the display tree. The second any of the children changes, your build is now broke, and you won't know that until runtime because it's a module.

What you need is an application framework. A way to increase complexity without decreasing maintainability. There are many out there, but my personal favorite is Parsley. I've used it on many very large projects with much success. The problem you're trying to solve right now, dispatching one event where the other module listens for it, is extremely trivial (can be done in about 3 lines of code).

I recommend you look it over as well as my presentation on an introduction to parsley.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top