Skip to content

Instantly share code, notes, and snippets.

@pukhalski
Created May 16, 2013 11:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save pukhalski/5590943 to your computer and use it in GitHub Desktop.
Save pukhalski/5590943 to your computer and use it in GitHub Desktop.
var APP = APP || {};
APP.EventBus = {};
APP.EventBus.bind = function (ev, callback, context) {
var calls = this._callbacks || (this._callbacks = {});
var list = calls[ev] || (calls[ev] = []);
list.push([callback, context]);
return this;
};
APP.EventBus.unbind = function (ev, callback) {
var calls;
if (!ev) {
this._callbacks = {};
} else if (calls = this._callbacks) {
if (!callback) {
calls[ev] = [];
} else {
var list = calls[ev];
if (!list) return this;
for (var i = 0, l = list.length; i < l; i++) {
if (list[i] && callback === list[i][0]) {
list[i] = null;
break;
}
}
}
}
return this;
};
APP.EventBus.trigger = function (eventName) {
var list, calls, ev, callback, args;
var both = 2;
if (!(calls = this._callbacks)) return this;
while (both--) {
ev = both ? 'all' : eventName;
if (list = calls[ev]) {
for (var i = 0, l = list.length; i < l; i++) {
if (!(callback = list[i])) {
list.splice(i, 1);
i--;
l--;
} else {
args = both ? arguments : Array.prototype.slice.call(arguments, 1);
callback[0].apply(callback[1] || this, args);
}
}
}
}
return this;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment