Skip to content

Instantly share code, notes, and snippets.

@sh4n3d4v15
Created September 30, 2012 11:32
Show Gist options
  • Select an option

  • Save sh4n3d4v15/3806517 to your computer and use it in GitHub Desktop.

Select an option

Save sh4n3d4v15/3806517 to your computer and use it in GitHub Desktop.

Revisions

  1. sh4n3d4v15 created this gist Sep 30, 2012.
    18 changes: 18 additions & 0 deletions gistfile1
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    // 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");
    });