Last active
August 29, 2015 13:57
-
-
Save joelgriffith/9472872 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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