gskinner.com: gBlog: FlashMX 2004 source code, news, reviews and opinions
Back to gBlog Main gskinner.com website: source code, portfolio, etc. contact Grant Skinner

Syndication
RSS 1.0
RSS 2.0


Subscribe
Enter your email address to be notified when posts are added.

Search

Resources
Conference Session Notes

Flash Blogs
Waxpraxis
Philterblog
W3Blog
Jonas Galvez
Josh Dura
Quasimondo
Flashguru
Sean Voisen
Colin Moock
Flazoom
Greg Burch
Pope De Flash
Peter Hall
Glyn Thomas
actionscript.com
Princess Pegg


Aggregators
Flog
FullAsAGoog
MXNA
Hall of Justhese


« Event dispatcher, the next morning | Back to Main | GDispatcher bug fixed. »

September 29, 2003


IMPORTANT NOTE: This is an old archive. It is only here to support outdated external links. To view the updated version of this archive, please go to the blog index, and search for the title of this document using the search form.

[code] GDispatcher: Event swiss army knife.
Posted by Grant
A few posts back, I mentioned that I was planning to extend the EventDispatcher class to add some features that I wanted. I quickly realized that to satisfy my needs, I would need to do a full re-write, and GDispatcher was born. Being the generous fellow that I am ;), I thought I'd share it with the community - read through this post to download it.

GDispatcher is a new event dispatching class for ActionScript 2.0. It fully implements the EventDispatcher interface, allowing it to be interchanged easily (yay polymorphism!). Beyond the basic functionality found in EventDispatcher (described here), it also supports these new features:
  • Listeners can subscribe to all events from a dispatcher
  • Listeners can specify a function to handle specific events, or all events (within the scope of the listener)
  • Can check if a listener is already subscribed to an event
  • Can remove all listeners for a specific event or for all events

Following, is some limited documentation on using the new features. When I get some time, I will write up a post on actually implementing it in a project, though you can get a pretty good idea by reading my last couple of posts on the subject (Using EventDispatcher, EventDispatcher: the next morning).

Subscribing to all events:
This can be done just as though you were subscribing to any other event, but you pass "ALL" instead of an event name:

someObject.addEventListener("ALL",this);

You can also remove a listener for all events as with any other event:

someObject.removeEventListener("ALL",this);

Note that you are really subscribing to an ALL event, that is triggered by any event call from the dispatching object (someObject). If you subscribe to other specific events from the same object, you will receive duplicate calls. Similarly, you cannot subscribe only to the ALL event, and then remove specific events.

Specifying a handler function:
By passing an optional third parameter in an addEventListener call, you can specify the name of a function to call. This function will be called within the scope of the object specified in the second parameter:

someObject.addEventListener("click",this,"onClicked");

This can of course be combined with other new features, like listening to "ALL" events:

someObject.addEventListener("ALL",this,"onSomeEvent");

To remove the listener, all parameters must be the same:

// will not remove the listener:
someObject.removeEventListener("ALL",this);
// will remove it:
someObject.removeEventListener("ALL",this,"onSomeEvent");

Checking for a listener:
GDispatcher adds the "eventListenerExists" method to check if a listener is already subscribed to a certain event:

if (someObject.eventListenerExists("click",this)) {
   trace("Yes, it's subscribed");
} else {
   trace("No, it isn't subscribed");
}

Again, the listener details must match exactly to the details used to subscribe it.

Removing all listeners to an event
The added "removeAllEventListeners" removes all event listeners from a dispatcher for a specific event, or for all events.

For example, to remove all listeners to the click event for someObject:

someObject.removeAllListeners("click");

To remove all listeners that subscribe to the "ALL" event for someObject:

someObject.removeAllListeners("ALL");

To remove every listener for every event for someObject, simply do not specify an event name:

someObject.removeAllListeners();



Downloading GDispatcher
GDispatcher is in beta, but I would like as many people to try it out as possible. I can't offer support for it, and will warn you in advance that you will need a basic understanding of how EventDispatcher works to use GDispatcher successfully, at least until I get the time to post more in-depth docs.

I would really appreciate it if coders could report bugs, or request features for gDispatcher in the comments, or by emailing them to me (there's an address in the comments at the top of the AS file you can use).

Please do not re-distribute GDispatcher at this time (I want to finalize it, and comment the code first).

Click here to download GDispatcher beta 1. (.zip containing a single .as file, 1.5kb)

Posted @ 09:47 AM by Grant | TrackBack


Comments

Ahh, the power of G. I'll play around with it tonight.

Posted by: Funkatron at September 29, 2003 02:18 PM

Very nice Grant! MMs EventDispatcher was nice but your additions make it REALLY nice. Thanks for sharing this the community!

I am to pretty deep into an MX2004 project right now and am affraid of rewiring things to use GDispatcher at this point but I will for sure be trying it out on the next project I start!!

Thanks Grant!

Posted by: Lance Linder at September 29, 2003 03:28 PM

I'd rather use an enumerated type for events, instead of String

Posted by: pmpngn at December 9, 2003 05:16 PM

I hope I am not stepping on any toes here. I went ahead and developed my own EventDispatcher for AS1 simulating the F04 model. I want to use the same model for F6 components.

I thought you might like to look at the code; it might offer you some ideas for your gDispatcher. Also, feel welcome to blast my implementation. I Haven't even downloaded the Demo vers. of F04, so I haven't seen the actual code for Macromedia's EventDispatcher. I guess this means my StEventDispatcher will stay in AS1 component form for the time being.

I have an EventDispatcher demo application that you might find interesting.

StEventDispatcher code, app and reference info is all online here:
http://www.shocktime.com/eventdispatcher

Posted by: philip at December 11, 2003 06:03 PM

String is better how could u use an enumerated type for events ? It's not clear!!!

Posted by: SuOnErIe at January 16, 2004 10:11 PM

You should add another method .addUniqueEventListener( event, scope, method )

Basically, it's a helper function that would just check if the listener already exists and adds it if it doesn't. Would just lend itself to cleaner code.

Cheers Grant,
chicken

Posted by: chicken at September 8, 2004 07:31 PM

Hosting by NetKeepers.ca | Powered by Movable Type 2.661
The text content of this blog is licensed under a Creative Commons License. Graphics are ©2003 Grant Skinner.