Skip to content

Instantly share code, notes, and snippets.

@ngsankha
Last active December 19, 2015 01:19
Show Gist options
  • Save ngsankha/5875354 to your computer and use it in GitHub Desktop.
Save ngsankha/5875354 to your computer and use it in GitHub Desktop.
Simple event handling mechanism
"use strict";
/* EventEmitter - simple event handling
*
* Use var myObj = Object.create(EventEmitter) to inherit the functions
* Use myObj.on('event', callback) to attach event handlers
* Use emit('event') to trigger the event handlers
*/
var EventEmitter = {
handlers: {},
emit: function (event, data) {
if (this.handlers.hasOwnProperty(event)) {
for (var i = 0; i < this.handlers[event].length; i++)
this.handlers[event][i](data);
}
},
on: function (event, callback) {
if (this.handlers.hasOwnProperty(event))
this.handlers[event].push(callback);
else
this.handlers[event] = [callback];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment