Skip to content

Instantly share code, notes, and snippets.

@ivillamil
Created November 28, 2012 15:53
Show Gist options
  • Save ivillamil/4162131 to your computer and use it in GitHub Desktop.
Save ivillamil/4162131 to your computer and use it in GitHub Desktop.
PubSub [observer pattern with jQuery]
/*
| Include this snippet before your scripts in order to get access to the pubSub pattern
| USAGE:
| $.publish('myApp:customEvent');
| $.suscribe('myApp:customEvent', yourCallbackFunction);
**/
(function( $ ) {
var o = $( {} );
$.each({
trigger: 'publish',
on: 'suscribe',
off: 'unsuscribe'
},function(key, val){
jQuery[val] = function() {
o[key].apply(o, arguments );
}
});
})( jQuery );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment