#Events
This library is intended to supply an event interface to javascript objects using a Mixin pattern. The library is designed with encapsulation and uninterupptability in mind. This means a broken function will not stop the event loop (on modern browsers).
##Features
- Easy to integrate - can be used as a mixin, thus making it easy to integrate with any existing codebase.
- Fast - On modern browsers (FF, Chrome, Safari and IE > 8) the library uses costume DOM events to handle the events.
- Encapsulated - When used on modern browsers, broken functions will not break the event loop.
- Supports creating pseudo events (like
:once
and:latched
). - Comes with built in useful utilities.
- Cross-browser - falls back to a more standard costume events handler.
##Usage
The simplest way to enable events support is like this:
function MyObject(){
Events.call(this);
}
This method will introduce the entire events API onto the object. You could also create an instance of the object:
var ev = new Events();
Or, if you wish, you could always inherit it:
obj.prototype = new Events();
##Methods
###addEvent(type, fn) Adds a listener. When fired, the function will be passed an event object, with 2 parameters:
- args - arguments that were passed by dispatcher.
- dispatcher - the object dispatching the event
obj.addEvent('show',function(e){
e.dispatcher; //obj
e.args; //whatever arguments were passed
});
###fireEvent(type, args) Dispathces an event, passing arguments:
obj.fireEvent('show', {counter:1});
###removeEvent(type, fn) Removes a listener.
obj.removeEvent('show', this.bound.handleShow);
###addEventOnce(type, fn)
Same as addEvent
only it will remove the listener automatically once dispatched.
##Pseudo events The library supports a few pseudo events out of the box:
:once
- when used event will be added once.:latched
- see latched section:times(number)
- same as once, only it will execute X times (as passed by parameter).:delayed(ms)
- onfireEvent
, will delay X miliseconds before firing the event. OnaddEvent
will delay each execution of specific function.
obj.addEvent('test:once', function(){/* ... */ });
obj.fireEvent('load:latched');
obj.addEvent('test:times(5)', fn);
You can also add your own pseudo events, by adding them to Events.Pseudoes.
In order to create a new pseudo-event, add an object to the collection, containing either addEvent
method, fireEvent
method or both.
You can even add a parameter to the pseudo-event.
For example:
Events.Pseudoes.delayed = {
addEvent : function(type, fn, time){
this.addEvent(type, function(){
setTimeout(fn, time);
});
}
};
This will allow you to do:
obj.addEvent('test:delayed(1000)', fn);//will add a delayed event
The addEvent
and fireEvent
methods will be fired instead of the default methods. It's arguments will be the same as their default, with a third argument, which is the passed pseudo-parameter (if any).
##Latched events Latched events are events that once fired once will dispatch automatically afterwards. Examples for such events can be a 'load' event, or a 'domready' event. If any arguments were passed, they will be passed on as well. For example:
obj.fireEvent('load:latched',{someParam:"a"});
//will be fire automatically
obj.addEvent('load', function(e){
e.args.someParam; //a
});
##Cleanup
In case you want to ensure cleanup, the Mixin automatically listens to the destroy
event and cleans itself up for destruction