Skip to content

Instantly share code, notes, and snippets.

@clemgrim
Last active August 29, 2015 14:14
Show Gist options
  • Save clemgrim/2956a9e564cf8555b745 to your computer and use it in GitHub Desktop.
Save clemgrim/2956a9e564cf8555b745 to your computer and use it in GitHub Desktop.
(function($) {
var o = $({});
$.subscribe = function() {
o.on.apply(o, arguments);
};
$.unsubscribe = function() {
o.off.apply(o, arguments);
};
$.publish = function() {
o.trigger.apply(o, arguments);
};
}(jQuery));
// Usage:
// Super-basic example:
function handle(e, a, b, c) {
// `e` is the event object, you probably don't care about it.
console.log(a + b + c);
};
$.subscribe("/some/topic", handle);
$.publish("/some/topic", [ "a", "b", "c" ]);
// logs: abc
$.unsubscribe("/some/topic", handle); // Unsubscribe just this handler
// Or:
$.subscribe("/some/topic", function(e, a, b, c) {
console.log(a + b + c);
});
$.publish("/some/topic", [ "a", "b", "c" ]);
// logs: abc
// Unsubscribe all handlers for this topic
$.unsubscribe("/some/topic");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment