Skip to content

Instantly share code, notes, and snippets.

@Tom32i
Created June 3, 2014 10:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Tom32i/750bcbbea21434b633b0 to your computer and use it in GitHub Desktop.
Save Tom32i/750bcbbea21434b633b0 to your computer and use it in GitHub Desktop.
Simple EventEmitter Object
/**
* Event Emitter
*/
function EventEmitter ()
{
this.element = document.createElement('div');
}
/**
* Emit a new event
*
* @param {String} type
* @param {Object} data
*/
EventEmitter.prototype.emit = function(type, data)
{
this.element.dispatchEvent(new CustomEvent(type, {detail: data}));
};
/**
* Add a listener
*
* @param {String} name
* @param {Function} callback
*/
EventEmitter.prototype.addEventListener = function(name, callback)
{
this.element.addEventListener(name, callback, false);
};
/**
* Remove a listener
*
* @param {String} name
* @param {Function} callback
*/
EventEmitter.prototype.removeEventListener = function(name, callback)
{
this.element.removeEventListener(name, callback, false);
};
/**
* On alias
*/
EventEmitter.prototype.on = EventEmitter.prototype.addEventListener;
/**
* Off alias
*/
EventEmitter.prototype.off = EventEmitter.prototype.removeEventListener;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment