Created
July 2, 2015 20:25
-
-
Save ivan-loh/c68679457ccd742e22b4 to your computer and use it in GitHub Desktop.
simple javascript event bus for handling lots of stuff
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
| 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