Skip to content

Instantly share code, notes, and snippets.

@simenbrekken
Created May 3, 2012 09:35
Show Gist options
  • Save simenbrekken/2584742 to your computer and use it in GitHub Desktop.
Save simenbrekken/2584742 to your computer and use it in GitHub Desktop.
jQuery pub/sub plugin with context
(function($) {
var observer = $({});
$.subscribe = function(topic, handler, context) {
observer.on(topic, $.proxy(handler, context));
};
$.unsubscribe = function(topic, handler, context) {
observer.off(topic, $.proxy(handler, context));
};
$.publish = function(topic) {
observer.trigger.apply(observer, [topic].concat([Array.prototype.slice.call(arguments).slice(1)]));
};
}(jQuery));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment