SHARE
TWEET

Sexy PubSub

a guest Feb 21st, 2012 4,797 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Works in modern browsers + IE9, but Modernizr has a polyfill baked in for function.bind.
  2. // Hat tip Paul Irish
  3.  
  4. var o = $( {} );
  5. $.subscribe = o.on.bind(o);
  6. $.unsubscribe = o.off.bind(o);
  7. $.publish = o.trigger.bind(o);
  8.  
  9. // Usage
  10. $(document.body).on( 'click', function() {
  11.    // ...yadada
  12.    $.publish( 'clicketyClack' ); // Think Rocky Balboa yelling out the window: "Hey yo!"
  13. });
  14.  
  15. // And some dude listening patiently for Rocky's voice.
  16. $.subscribe( 'clicketyClack', function() {
  17.         console.log("You can't win, Rock");
  18. });
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top