I have a simple object that get's geocoding data from the Google Maps API and I'd like it to dispatch a set of custom events depending on the response from Google. However, I don't have the addEventListener() and dispatchEvent() methods on this class because it doesn't extend UIComponent. Is there a way to create custom events in Flex 3 without inheriting from UIComponent?
+1
A:
Extend the EventDispatcher class, or implement the IEventDispatcher interface from the flash.events package.
mmattax
2009-01-14 21:33:14
+1
A:
Yes, that should be possible. Extend EventDispatcher like this:
import flash.events.*;
public class MyDispatcher extends EventDispatcher {
private function doIt(event:Event):void {
dispatchEvent(new Event("myEvent"));
}
Martin Wickman
2009-01-14 21:41:31
+7
A:
Absolutely yes. To take advantage of event handling, your custom object should extend EventDispatcher:
public class MyClass extends EventDispatcher
{
// ...
public function myFunction():void
{
dispatchEvent(new Event("myEvent"));
}
}
This takes care of dispatching events.
Then as well, if you have the need, you can also create a separate class that extends Event, which gives you the option (among other things) of attaching event-specific data for use in the handler:
public class MyClass extends EventDispatcher
{
// ...
public function myFunction():void
{
dispatchEvent(new MyEvent(someData));
}
}
public class MyEvent extends Event
{
private var _myEventData:Object;
public function MyEvent(eventData:Object)
{
_myEventData = eventData;
}
}
.. so then in your handler, you'd just:
private function myHandler(event:MyEvent):void
{
trace(event.myEventData.toString());
}
Make sense?
Christian Nunciato
2009-01-14 21:46:08
Thanks man...you just saved a Flex newbie ;)
JC Grubbs
2009-01-14 21:48:02
My pleasure. Good luck!
Christian Nunciato
2009-01-14 21:55:50
A:
That's exactly what I did! extended EventDispatcher and inherited dispatchEvent
Thanks lads
PirosB3
2010-10-22 13:25:12