Created
December 16, 2013 22:38
-
-
Save stevenschobert/7995724 to your computer and use it in GitHub Desktop.
If you ever need to whip up a quick event emitter, and you already have jQuery, you can piggyback off jQuery's 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
| // event emitter factory built off jQuery events | |
| eventEmitter = function eventEmitter () { | |
| var pipe = $({}), | |
| api = { | |
| trigger: function trigger (event, data) { | |
| pipe.trigger({ | |
| type: event, | |
| customData: data | |
| }); | |
| }, | |
| on: function on (event, handler) { | |
| pipe.on(event, function (e) { | |
| handler(e.customData); | |
| }); | |
| }, | |
| off: function off (event) { | |
| pipe.off(event); | |
| }, | |
| once: function once (event, handler) { | |
| pipe.one(event, function (e) { | |
| handler(e.customData); | |
| }); | |
| } | |
| }; | |
| return api; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment