Skip to content

Instantly share code, notes, and snippets.

@joelgriffith
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joelgriffith/9472872 to your computer and use it in GitHub Desktop.
Save joelgriffith/9472872 to your computer and use it in GitHub Desktop.
/*
* Module Wrapper
*/
(function (base, factory) {
'use strict';
// RequireJS
if (typeof define === 'function' && define.amd) {
define(factory);
// CommonJS
} else if (typeof exports === 'object') {
module.exports = factory();
// Global Space
} else {
base.EventEmitter = factory();
}
}(this, function () {
'use strict';
/**
* Event Emitter
* The Emitter Class/Constructor
*/
function EventEmitter() {
// Event Storage
this._events = [];
/**
* On
* Registers a private event to fire later
*/
this.on = function (evt, callback) {
var e = {};
e.evt = evt;
e.callback = callback;
this._events.push(e);
};
/**
* Off
* Removes event from the event registry
*/
this.off = function (evt) {
var events = this._events;
for (var i = 0; i < events.length; i++) {
var e = events[i];
if (e.evt === evt) { events.splice(i, 1); }
}
};
/*
* Fire Event
* Calls the the appropriate event in the registry
*/
this.trigger = function (evt, data) {
var events = this._events;
for (var i = 0; i < events.length; i++) {
var e = events[i];
if (e.evt === evt) {
e.callback(data);
}
}
};
}
return EventEmitter;
}));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment