Created
March 13, 2015 00:50
-
-
Save d-simon/33750128520fb4d897b5 to your computer and use it in GitHub Desktop.
jQuery Mediator / Event Dispatcher
This file contains 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($) { | |
'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