3

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

Ganapa
  • 73
  • 1
  • 9
  • How is this related to Orbeon? (I am asking as you put the tag Orbeon on this question.) – avernet Mar 23 '11 at 06:30
  • but i have used same mechanism once befroe and it was working all fine.. the event which is thrown is of type AlgoEvent.GETFROMPARENT_LOCAL_EVENT only but while on the other side while handling the prototype og the event handler i.e public function sendParametersToChild(e:AlgoEvent):void gives the error :e:AlgoEvent is the root cause for this error.. if i use e:Event instead of e:AlgoEvent it will work fine .. but me want it to be e:AlgoEvent only as am attaching some data with AlgoEvent to access it in the other side.. – Ganapa Mar 23 '11 at 06:45

7 Answers7

3

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));
splash
  • 13,037
  • 1
  • 44
  • 67
  • @user672468: If you are using Firefox then you should clean up your browser cache to make sure that the changes are in effect. – splash Mar 23 '11 at 09:33
  • Thank you..Me got the solution with your "Try to make the AlgoEvent class known to the main application" approach.. Me had just imported it in my main application.. But now i tried with craeting a variable of Algo Event in Main Application Simply.. And it worked!!! Thank you so much.. – Ganapa Mar 23 '11 at 10:34
  • ok.. as i need some data to be sent with the event i had to create a cusum event's instatnce only.. – Ganapa Mar 23 '11 at 11:03
2

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

Jorge
  • 21
  • 1
1

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/

Daniel Hedberg
  • 5,677
  • 4
  • 36
  • 61
Ganapa
  • 73
  • 1
  • 9
0

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

Neeraj
  • 8,408
  • 8
  • 41
  • 69
0

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.

himanshu
  • 392
  • 3
  • 17
  • Added the code but it has no effects.. am not able to understand what type of conversion is requiered here.. TypeError: Error #1034: Type Coercion failed: cannot convert resources.events::AlgoEvent@63690b1 to resources.events.AlgoEvent.... am throwing the same Algo Event and Catching the same in the handler.. – Ganapa Mar 23 '11 at 08:50
0

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

robertp
  • 3,557
  • 1
  • 20
  • 13
-1

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.

J_A_X
  • 12,857
  • 1
  • 25
  • 31