Event DispatcherCustom events is a need for many applications and since the whole RIA revolution that need is getting satisfied in many ways. I have looked at those who have come before me in this battlefield and I have deemed the model found in Action Script to be favorable. ActionScript's interfaceActionScript defined four public methods for an event dispatcher class. All of which are identical in the EventDispatcher class written for javascript which can be found below.
By leveraging an already proven model from ActionScript and prototype.js' ability to mimic class behavior, including abstract class behavior we can create a base class from which concrete classes can extend from. In the code below I have attempted to avoid an initialize function to keep things "abstract". By doing this the implementation doesn't need to worry about initializing anything. The Code
/**
* @author Matthew Foster
* @date June 6th 2007
* @purpose To have a base class to extend subclasses from to inherit event dispatching functionality.
* @procedure Use a hash of event "types" that will contain an array of functions to execute. The logic is if any function explicitally returns false the chain will halt execution.
*/
var EventDispatcher = function(){};
Object.extend(EventDispatcher.prototype,
{
buildListenerChain : function(){
if(!this.listenerChain)
this.listenerChain = {};
},
addEventListener : function(type, listener){
if(!listener instanceof Function)
throw { message : "Listener isn't a function" };
this.buildListenerChain();
if(!this.listenerChain[type])
this.listenerChain[type] = [listener];
else
this.listenerChain[type].push(listener);
},
hasEventListener : function(type){
return (typeof this.listenerChain[type] != "undefined");
},
removeEventListener : function(type, listener){
if(!this.hasEventListener(type))
return false;
for(var i = 0; i < this.listenerChain[type].length; i++)
if(this.listenerChain[type][i] == listener)
this.listenerChain.splice(i, 1);
},
dispatchEvent : function(type, args){
this.buildListenerChain();
if(!this.hasEventListener(type))
return false;
this.listenerChain[type].any(function(f){ return (f(args) == false ? true : false); });
}
}
);
Check out how I built a great Grid API with this class.Event PropogationYou can stop the propogation of the event through the event chain by explicitly returning false from any of the listeners. Argument(s)The dispatchEvent method only accepts one variable as the "arguments" for the listening function. The idea behind this is either bundle your information into an object (Like an Event object with target and currentTarget properties) or you can bundle a series of objects into an array, then using apply you can resituate the variables for a function expecting parameterized arguments. Other information
|
||
CommentsSeptember 18, 2008Guncel driver download sitesi - actual driver download site Thanks September 22, 2008nakliyat thank you September 23, 2008web tasarım web tasarım September 27, 2008Games Thanks October 10, 2008evden eve nakliyat thanksss October 20, 2008cam balkon thanks October 21, 2008penis büyütücü Thank you very much for this information. October 25, 2008cam balkon thanks October 29, 2008figurin Thank you very much for this information. November 24, 2008betsson Thanks for information. November 24, 2008ftkcambalkon cam balkon imalatçısı November 28, 2008betsson Thanks for information. December 04, 2008ask siirleri ask siirleri January 13, 2009Plinian Hjemmeside Hosting EventDispatcher is a class which provides a means of dispatching events. After you’ve recovered from that bombshell of an introduction sentence, let’s get more in depth. EventDispatcher, like AsBroadcaster broadcasts an event to a set of listeners which then receive that event and call the function associated with it. January 14, 2009weight loss pills weight loss pills January 18, 2009Industrial PC enclosures There are several places where RSSReader and HorizontalTicker will need to notify the Controller; for example, when the RSS feed is properly loaded, the controller will need to start the ticker. The question here is how to glue everything together. January 20, 2009cam balkon thank you admin. January 21, 2009Web Design Birmingham The EventDispatcher class is the base class for all classes that dispatch events. The EventDispatcher class implements the IEventDispatcher interface and is the base class for the DisplayObject class. The EventDispatcher class allows any object on the display list to be an event target and as such, to use the methods of the IEventDispatcher interface. March 29, 2009cam balkon thankssss June 22, 2009fendsell www.fendsell.com July 20, 2009CAM BALKON cam balkon July 22, 2009cam balkon cam balkon September 16, 2009Replica Watches Today,i bought September 19, 2009fendsell www.fendsell.com September 19, 2009Anadolu Cam Balkon http://www.anadolucambalkon.net October 11, 2009wholesale Very useful info! October 29, 2009cam balkon herkesi gorunce bende katıldım:) |
||
Thanks!