Skip to content

Instantly share code, notes, and snippets.

@iainmcampbell
Created December 18, 2014 22:16
Show Gist options
  • Save iainmcampbell/f3f825b72f410526aa66 to your computer and use it in GitHub Desktop.
Save iainmcampbell/f3f825b72f410526aa66 to your computer and use it in GitHub Desktop.
tiny pubsub
(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:

$.subscribe('eventName', function(data){ console.log(data.key) });
$.publish('eventName', { key: value });

// good idea: keep track of events using an array

events = {
    eventName : 'EVENTNAME'
}

$.subscribe(events.eventName)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment