Skip to content

Instantly share code, notes, and snippets.

@d-simon
Created March 13, 2015 00:50
Show Gist options
  • Save d-simon/33750128520fb4d897b5 to your computer and use it in GitHub Desktop.
Save d-simon/33750128520fb4d897b5 to your computer and use it in GitHub Desktop.
jQuery Mediator / Event Dispatcher
(function($) {
'use strict';
var $document = $(document.documentElement);
// Create the dispatcher
$.dispatcher = $.dispatcher || {};
var dispatcherMethods = {
trigger: function (event, data, elem) {
// If element is provided trigger from element
if (elem) {
// Wrap in jQuery and call trigger
return $(elem).trigger(event, data);
} else {
return $document.trigger(event, data);
}
},
on: function (event, callback, scope) {
return $document.on(event, $.proxy(callback, scope || $document));
},
off: function (event) {
return $document.off(event);
}
}; // dispatcherMethods end
// Attach the dispatcher methods to $.dispatcher
$.extend(true, $.dispatcher, dispatcherMethods);
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment