Created
March 25, 2015 16:20
-
-
Save JordanForeman/d14f7657d95ba66f764f to your computer and use it in GitHub Desktop.
Basic Custom Events
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
| EventDispatcher = { | |
| events: {}, | |
| on: function(event, callback) { | |
| var handlers = this.events[event] || []; | |
| handlers.push(callback); | |
| this.events[event] = handlers; | |
| }, | |
| trigger: function(event, data) { | |
| var handlers = this.events[event]; | |
| if (!handlers || handlers.length < 1) | |
| return; | |
| [].forEach.call(handlers, function(handler){ | |
| handler(data); | |
| }); | |
| } | |
| }; |
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
| function initializeListeners() { | |
| EventDispatcher.on('fire', fire); // fire.bind(this) -- if necessary | |
| } | |
| function fire(x) { | |
| console.log(x); | |
| } | |
| function thingHappened(thing) { | |
| EventDispatcher.trigger('fire', thing); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment