Skip to content

Instantly share code, notes, and snippets.

@sv-in
Forked from furf/jquery.ba-tinypubsub.js
Created October 16, 2012 11:14

Revisions

  1. @furf furf revised this gist Oct 27, 2011. No changes.
  2. @furf furf revised this gist Oct 27, 2011. 2 changed files with 13 additions and 15 deletions.
    26 changes: 12 additions & 14 deletions jquery.ba-tinypubsub.js
    Original file line number Diff line number Diff line change
    @@ -2,20 +2,18 @@
    * http://benalman.com/
    * Copyright (c) 2011 "Cowboy" Ben Alman; Licensed MIT, GPL */

    (function($) {
    (function($, o){

    var o = $({});
    o = $({});

    $.subscribe = function() {
    o.on.apply(o, arguments);
    };
    $.each({
    subscribe: 'on',
    unsubscribe: 'off',
    publish: 'trigger'
    }, function (k, v) {
    $[k] = function() {
    o[v].apply(o, arguments);
    };
    });

    $.unsubscribe = function() {
    o.off.apply(o, arguments);
    };

    $.publish = function() {
    o.trigger.apply(o, arguments);
    };

    }(jQuery));
    })(jQuery);
    2 changes: 1 addition & 1 deletion jquery.ba-tinypubsub.min.js
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    /* jQuery Tiny Pub/Sub - v0.7 - 10/27/2011
    * http://benalman.com/
    * Copyright (c) 2011 "Cowboy" Ben Alman; Licensed MIT, GPL */
    (function(a){var b=a({});a.subscribe=function(){b.on.apply(b,arguments)},a.unsubscribe=function(){b.off.apply(b,arguments)},a.publish=function(){b.trigger.apply(b,arguments)}})(jQuery)
    (function(a,b){b=a({});a.each({subscribe:"on",unsubscribe:"off",publish:"trigger"},function(d,c){a[d]=function(){b[c].apply(b,arguments)}})})(jQuery)
  3. @cowboy cowboy revised this gist Oct 27, 2011. 2 changed files with 12 additions and 44 deletions.
    45 changes: 9 additions & 36 deletions jquery.ba-tinypubsub.js
    Original file line number Diff line number Diff line change
    @@ -1,48 +1,21 @@
    /*!
    * jQuery Tiny Pub/Sub - v0.6 - 1/10/2011
    /* jQuery Tiny Pub/Sub - v0.7 - 10/27/2011
    * http://benalman.com/
    *
    * Copyright (c) 2010 "Cowboy" Ben Alman
    * Dual licensed under the MIT and GPL licenses.
    * http://benalman.com/about/license/
    */
    * Copyright (c) 2011 "Cowboy" Ben Alman; Licensed MIT, GPL */

    (function($){
    (function($) {

    // Create a "dummy" jQuery object on which to bind, unbind and trigger event
    // handlers. Note that $({}) works in jQuery 1.4.3+, but because .unbind on
    // a "plain object" throws errors in older versions of jQuery, an element is
    // used here.
    var o = $('<b/>');
    var o = $({});

    // Subscribe to a topic. Works just like bind, except the passed handler
    // is wrapped in a function so that the event object can be stripped out.
    // Even though the event object might be useful, it is unnecessary and
    // will only complicate things in the future should the user decide to move
    // to a non-$.event-based pub/sub implementation.
    $.subscribe = function( topic, fn ) {

    // Call fn, stripping out the 1st argument (the event object).
    function wrapper() {
    return fn.apply( this, Array.prototype.slice.call( arguments, 1 ) );
    }

    // Add .guid property to function to allow it to be easily unbound. Note
    // that $.guid is new in jQuery 1.4+, and $.event.guid was used before.
    wrapper.guid = fn.guid = fn.guid || ( $.guid ? $.guid++ : $.event.guid++ );

    // Bind the handler.
    o.bind( topic, wrapper );
    $.subscribe = function() {
    o.on.apply(o, arguments);
    };

    // Unsubscribe from a topic. Works exactly like unbind.
    $.unsubscribe = function() {
    o.unbind.apply( o, arguments );
    o.off.apply(o, arguments);
    };

    // Publish a topic. Works exactly like trigger.
    $.publish = function() {
    o.trigger.apply( o, arguments );
    o.trigger.apply(o, arguments);
    };

    })(jQuery);
    }(jQuery));
    11 changes: 3 additions & 8 deletions jquery.ba-tinypubsub.min.js
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,4 @@
    /*
    * jQuery Tiny Pub/Sub - v0.6 - 1/10/2011
    /* jQuery Tiny Pub/Sub - v0.7 - 10/27/2011
    * http://benalman.com/
    *
    * Copyright (c) 2010 "Cowboy" Ben Alman
    * Dual licensed under the MIT and GPL licenses.
    * http://benalman.com/about/license/
    */
    (function($){var a=$("<b/>");$.subscribe=function(b,c){function d(){return c.apply(this,Array.prototype.slice.call(arguments,1))}d.guid=c.guid=c.guid||($.guid?$.guid++:$.event.guid++);a.bind(b,d)};$.unsubscribe=function(){a.unbind.apply(a,arguments)};$.publish=function(){a.trigger.apply(a,arguments)}})(jQuery);
    * Copyright (c) 2011 "Cowboy" Ben Alman; Licensed MIT, GPL */
    (function(a){var b=a({});a.subscribe=function(){b.on.apply(b,arguments)},a.unsubscribe=function(){b.off.apply(b,arguments)},a.publish=function(){b.trigger.apply(b,arguments)}})(jQuery)
  4. @cowboy cowboy revised this gist Jan 10, 2011. 2 changed files with 24 additions and 8 deletions.
    28 changes: 22 additions & 6 deletions jquery.ba-tinypubsub.js
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    /*!
    * jQuery Tiny Pub/Sub - v0.5 - 1/9/2011
    * jQuery Tiny Pub/Sub - v0.6 - 1/10/2011
    * http://benalman.com/
    *
    * Copyright (c) 2010 "Cowboy" Ben Alman
    @@ -9,22 +9,38 @@

    (function($){

    var o = $({});

    // Create a "dummy" jQuery object on which to bind, unbind and trigger event
    // handlers. Note that $({}) works in jQuery 1.4.3+, but because .unbind on
    // a "plain object" throws errors in older versions of jQuery, an element is
    // used here.
    var o = $('<b/>');

    // Subscribe to a topic. Works just like bind, except the passed handler
    // is wrapped in a function so that the event object can be stripped out.
    // Even though the event object might be useful, it is unnecessary and
    // will only complicate things in the future should the user decide to move
    // to a non-$.event-based pub/sub implementation.
    $.subscribe = function( topic, fn ) {
    function handle() {

    // Call fn, stripping out the 1st argument (the event object).
    function wrapper() {
    return fn.apply( this, Array.prototype.slice.call( arguments, 1 ) );
    }

    handle.guid = fn.guid = fn.guid || handle.guid || $.guid++;
    // Add .guid property to function to allow it to be easily unbound. Note
    // that $.guid is new in jQuery 1.4+, and $.event.guid was used before.
    wrapper.guid = fn.guid = fn.guid || ( $.guid ? $.guid++ : $.event.guid++ );

    o.bind( topic, handle );
    // Bind the handler.
    o.bind( topic, wrapper );
    };

    // Unsubscribe from a topic. Works exactly like unbind.
    $.unsubscribe = function() {
    o.unbind.apply( o, arguments );
    };

    // Publish a topic. Works exactly like trigger.
    $.publish = function() {
    o.trigger.apply( o, arguments );
    };
    4 changes: 2 additions & 2 deletions jquery.ba-tinypubsub.min.js
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,9 @@
    /*
    * jQuery Tiny Pub/Sub - v0.5 - 1/9/2011
    * jQuery Tiny Pub/Sub - v0.6 - 1/10/2011
    * http://benalman.com/
    *
    * Copyright (c) 2010 "Cowboy" Ben Alman
    * Dual licensed under the MIT and GPL licenses.
    * http://benalman.com/about/license/
    */
    (function($){var o=$({});$.subscribe=function(topic,fn){function handle(){return fn.apply(this,Array.prototype.slice.call(arguments,1))}handle.guid=fn.guid=fn.guid||handle.guid||$.guid++;o.bind(topic,handle)};$.unsubscribe=function(){o.unbind.apply(o,arguments)};$.publish=function(){o.trigger.apply(o,arguments)}})(jQuery);
    (function($){var a=$("<b/>");$.subscribe=function(b,c){function d(){return c.apply(this,Array.prototype.slice.call(arguments,1))}d.guid=c.guid=c.guid||($.guid?$.guid++:$.event.guid++);a.bind(b,d)};$.unsubscribe=function(){a.unbind.apply(a,arguments)};$.publish=function(){a.trigger.apply(a,arguments)}})(jQuery);
  5. @cowboy cowboy revised this gist Jan 10, 2011. 2 changed files with 8 additions and 13 deletions.
    17 changes: 6 additions & 11 deletions jquery.ba-tinypubsub.js
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    /*!
    * jQuery Tiny Pub/Sub - v0.4 - 1/4/2011
    * jQuery Tiny Pub/Sub - v0.5 - 1/9/2011
    * http://benalman.com/
    *
    * Copyright (c) 2010 "Cowboy" Ben Alman
    @@ -11,19 +11,14 @@

    var o = $({});

    $.subscribe = function( type, data, fn ) {
    if ( $.isFunction( data ) || data === false ) {
    fn = data;
    data = undefined;
    }

    function proxy() {
    $.subscribe = function( topic, fn ) {
    function handle() {
    return fn.apply( this, Array.prototype.slice.call( arguments, 1 ) );
    };
    }

    proxy.guid = fn.guid = fn.guid || proxy.guid || $.guid++;
    handle.guid = fn.guid = fn.guid || handle.guid || $.guid++;

    o.bind( type, data, proxy );
    o.bind( topic, handle );
    };

    $.unsubscribe = function() {
    4 changes: 2 additions & 2 deletions jquery.ba-tinypubsub.min.js
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,9 @@
    /*
    * jQuery Tiny Pub/Sub - v0.4 - 1/4/2011
    * jQuery Tiny Pub/Sub - v0.5 - 1/9/2011
    * http://benalman.com/
    *
    * Copyright (c) 2010 "Cowboy" Ben Alman
    * Dual licensed under the MIT and GPL licenses.
    * http://benalman.com/about/license/
    */
    (function($){var o=$({});$.subscribe=function(type,data,fn){if($.isFunction(data)||data===false){fn=data;data=undefined}function proxy(){return fn.apply(this,Array.prototype.slice.call(arguments,1))}proxy.guid=fn.guid=fn.guid||proxy.guid||$.guid++;o.bind(type,data,proxy)};$.unsubscribe=function(){o.unbind.apply(o,arguments)};$.publish=function(){o.trigger.apply(o,arguments)}})(jQuery);
    (function($){var o=$({});$.subscribe=function(topic,fn){function handle(){return fn.apply(this,Array.prototype.slice.call(arguments,1))}handle.guid=fn.guid=fn.guid||handle.guid||$.guid++;o.bind(topic,handle)};$.unsubscribe=function(){o.unbind.apply(o,arguments)};$.publish=function(){o.trigger.apply(o,arguments)}})(jQuery);
  6. @cowboy cowboy revised this gist Jan 10, 2011. 2 changed files with 10 additions and 1 deletion.
    2 changes: 1 addition & 1 deletion jquery.ba-tinypubsub.js
    Original file line number Diff line number Diff line change
    @@ -27,7 +27,7 @@
    };

    $.unsubscribe = function() {
    o.unbind.apply( o, arguments ); // Seems to be broken in jQuery 1.4.4 (?)
    o.unbind.apply( o, arguments );
    };

    $.publish = function() {
    9 changes: 9 additions & 0 deletions jquery.ba-tinypubsub.min.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    /*
    * jQuery Tiny Pub/Sub - v0.4 - 1/4/2011
    * http://benalman.com/
    *
    * Copyright (c) 2010 "Cowboy" Ben Alman
    * Dual licensed under the MIT and GPL licenses.
    * http://benalman.com/about/license/
    */
    (function($){var o=$({});$.subscribe=function(type,data,fn){if($.isFunction(data)||data===false){fn=data;data=undefined}function proxy(){return fn.apply(this,Array.prototype.slice.call(arguments,1))}proxy.guid=fn.guid=fn.guid||proxy.guid||$.guid++;o.bind(type,data,proxy)};$.unsubscribe=function(){o.unbind.apply(o,arguments)};$.publish=function(){o.trigger.apply(o,arguments)}})(jQuery);
  7. @cowboy cowboy revised this gist Jan 5, 2011. 1 changed file with 22 additions and 11 deletions.
    33 changes: 22 additions & 11 deletions jquery.ba-tinypubsub.js
    Original file line number Diff line number Diff line change
    @@ -1,26 +1,37 @@
    /*!
    * jQuery Tiny Pub/Sub - v0.3 - 11/4/2010
    * jQuery Tiny Pub/Sub - v0.4 - 1/4/2011
    * http://benalman.com/
    *
    *
    * Copyright (c) 2010 "Cowboy" Ben Alman
    * Dual licensed under the MIT and GPL licenses.
    * http://benalman.com/about/license/
    */

    (function($){

    var o = $({});

    $.subscribe = function() {
    o.bind.apply( o, arguments );

    $.subscribe = function( type, data, fn ) {
    if ( $.isFunction( data ) || data === false ) {
    fn = data;
    data = undefined;
    }

    function proxy() {
    return fn.apply( this, Array.prototype.slice.call( arguments, 1 ) );
    };

    proxy.guid = fn.guid = fn.guid || proxy.guid || $.guid++;

    o.bind( type, data, proxy );
    };

    $.unsubscribe = function() {
    o.unbind.apply( o, arguments );
    o.unbind.apply( o, arguments ); // Seems to be broken in jQuery 1.4.4 (?)
    };

    $.publish = function() {
    o.trigger.apply( o, arguments );
    };
    })(jQuery);

    })(jQuery);
  8. @cowboy cowboy revised this gist Nov 15, 2010. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions jquery.ba-tinypubsub.js
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    /*!
    * jQuery Tiny Pub/Sub - v0.3pre - 11/4/2010
    * jQuery Tiny Pub/Sub - v0.3 - 11/4/2010
    * http://benalman.com/
    *
    * Copyright (c) 2010 "Cowboy" Ben Alman
    @@ -11,15 +11,15 @@

    var o = $({});

    $.subscribe = function(){
    $.subscribe = function() {
    o.bind.apply( o, arguments );
    };

    $.unsubscribe = function(){
    $.unsubscribe = function() {
    o.unbind.apply( o, arguments );
    };

    $.publish = function(){
    $.publish = function() {
    o.trigger.apply( o, arguments );
    };

  9. @cowboy cowboy revised this gist Nov 4, 2010. 1 changed file with 5 additions and 5 deletions.
    10 changes: 5 additions & 5 deletions jquery.ba-tinypubsub.js
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    /*!
    * jQuery Tiny Pub/Sub - v0.2pre - 11/3/2010
    * jQuery Tiny Pub/Sub - v0.3pre - 11/4/2010
    * http://benalman.com/
    *
    * Copyright (c) 2010 "Cowboy" Ben Alman
    @@ -9,18 +9,18 @@

    (function($){

    var w = $(window);
    var o = $({});

    $.subscribe = function(){
    w.bind.apply( w, arguments );
    o.bind.apply( o, arguments );
    };

    $.unsubscribe = function(){
    w.unbind.apply( w, arguments );
    o.unbind.apply( o, arguments );
    };

    $.publish = function(){
    w.trigger.apply( w, arguments );
    o.trigger.apply( o, arguments );
    };

    })(jQuery);
  10. @cowboy cowboy revised this gist Nov 3, 2010. 1 changed file with 5 additions and 5 deletions.
    10 changes: 5 additions & 5 deletions jquery.ba-tinypubsub.js
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    /*!
    * jQuery Tiny Pub/Sub - v0.1pre - 11/3/2010
    * jQuery Tiny Pub/Sub - v0.2pre - 11/3/2010
    * http://benalman.com/
    *
    * Copyright (c) 2010 "Cowboy" Ben Alman
    @@ -15,12 +15,12 @@
    w.bind.apply( w, arguments );
    };

    $.publish = function(){
    w.trigger.apply( w, arguments );
    $.unsubscribe = function(){
    w.unbind.apply( w, arguments );
    };

    $.unpublish = function(){
    w.unbind.apply( w, arguments );
    $.publish = function(){
    w.trigger.apply( w, arguments );
    };

    })(jQuery);
  11. @cowboy cowboy created this gist Nov 3, 2010.
    26 changes: 26 additions & 0 deletions jquery.ba-tinypubsub.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    /*!
    * jQuery Tiny Pub/Sub - v0.1pre - 11/3/2010
    * http://benalman.com/
    *
    * Copyright (c) 2010 "Cowboy" Ben Alman
    * Dual licensed under the MIT and GPL licenses.
    * http://benalman.com/about/license/
    */

    (function($){

    var w = $(window);

    $.subscribe = function(){
    w.bind.apply( w, arguments );
    };

    $.publish = function(){
    w.trigger.apply( w, arguments );
    };

    $.unpublish = function(){
    w.unbind.apply( w, arguments );
    };

    })(jQuery);