Skip to content

Instantly share code, notes, and snippets.

@Sigmus
Created January 30, 2014 18:11
Show Gist options
  • Save Sigmus/8714966 to your computer and use it in GitHub Desktop.
Save Sigmus/8714966 to your computer and use it in GitHub Desktop.
A simple event repository. (bugged)
var Events = {
registry: {},
nameExists: function(name) {
return typeof this.registry[name] !== 'undefined';
},
on: function(name, callback) {
if ( ! this.nameExists(name)) {
this.registry[name] = [];
}
this.registry[name].push(callback);
},
off: function(name) {
if (this.nameExists(name)) {
delete(this.registry[name]);
}
},
trigger: function(name, data) {
if ( ! this.nameExists(name)) {
return;
}
this.registry[name].forEach(function(callback) {
callback(data);
});
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment