Skip to content

Instantly share code, notes, and snippets.

@stevenschobert
Created December 16, 2013 22:38
Show Gist options
  • Select an option

  • Save stevenschobert/7995724 to your computer and use it in GitHub Desktop.

Select an option

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:
// 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