Advertisement
Guest User

Sexy PubSub

a guest
Feb 21st, 2012
5,093
0
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. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement