Skip to content

Instantly share code, notes, and snippets.

@ivan-loh
Created July 2, 2015 20:25
Show Gist options
  • Select an option

  • Save ivan-loh/c68679457ccd742e22b4 to your computer and use it in GitHub Desktop.

Select an option

Save ivan-loh/c68679457ccd742e22b4 to your computer and use it in GitHub Desktop.
simple javascript event bus for handling lots of stuff
var EventBus = (function () {
var topics = {};
return {
subscribe: function (topic, listener) {
if (!topics[topic]) {
topics[topic] = [];
}
topics[topic].push(listener);
},
publish: function (topic, data) {
var listeners = topics[topic];
if (!listeners || listeners.length < 1) {
return;
}
listeners
.forEach(function (listener) {
listener(data);
});
}
};
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment