Created
September 30, 2012 11:32
-
-
Save sh4n3d4v15/3806517 to your computer and use it in GitHub Desktop.
pubSubTrick
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
// Works in modern browsers + IE9, but Modernizr has a polyfill baked in for function.bind. | |
// Hat tip Paul Irish | |
var o = $( {} ); | |
$.subscribe = o.on.bind(o); | |
$.unsubscribe = o.off.bind(o); | |
$.publish = o.trigger.bind(o); | |
// Usage | |
$(document.body).on( 'click', function() { | |
// ...yadada | |
$.publish( 'clicketyClack' ); // Think Rocky Balboa yelling out the window: "Hey yo!" | |
}); | |
// And some dude listening patiently for Rocky's voice. | |
$.subscribe( 'clicketyClack', function() { | |
console.log("You can't win, Rock"); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment