Skip to content

Instantly share code, notes, and snippets.

@feng-ming
Forked from isao/jquery.ba-tinypubsub.js
Created January 15, 2013 05:42

Revisions

  1. @isao isao revised this gist Apr 21, 2011. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion jquery.ba-tinypubsub.js
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,7 @@
    * Dual licensed under the MIT and GPL licenses.
    * http://benalman.com/about/license/
    *
    * (removed pre 1.4.3 support, add $.pubsubdebug())
    * (removed pre 1.4.3 support, added $.pubsubdebug())
    */

    (function($){
  2. @isao isao revised this gist Apr 21, 2011. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion jquery.ba-tinypubsub.js
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,7 @@
    * Dual licensed under the MIT and GPL licenses.
    * http://benalman.com/about/license/
    *
    * (tweaked to remove pre 1.4.3 support, add $.pubsubdebug())
    * (removed pre 1.4.3 support, add $.pubsubdebug())
    */

    (function($){
  3. @isao isao revised this gist Apr 21, 2011. 2 changed files with 22 additions and 27 deletions.
    40 changes: 22 additions & 18 deletions jquery.ba-tinypubsub.js
    Original file line number Diff line number Diff line change
    @@ -1,48 +1,52 @@
    /*!
    * jQuery Tiny Pub/Sub - v0.6 - 1/10/2011
    * http://benalman.com/
    * http://benalman.com/ see https://gist.github.com/661855
    *
    * Copyright (c) 2010 "Cowboy" Ben Alman
    * Dual licensed under the MIT and GPL licenses.
    * http://benalman.com/about/license/
    *
    * (tweaked to remove pre 1.4.3 support, add $.pubsubdebug())
    */

    (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/>');
    // object on which to bind, unbind and trigger handlers. reqs jQuery 1.4.3+
    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 ) {
    $.subscribe = function(topic, fn) {

    // Call fn, stripping out the 1st argument (the event object).
    // Call fn, stripping out the 1st argument (the event object)
    function wrapper() {
    return fn.apply( this, Array.prototype.slice.call( arguments, 1 ) );
    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++ );
    // Add .guid property to function to allow it to be easily unbound
    // reqs jQuery 1.4+
    wrapper.guid = fn.guid = fn.guid || $.guid++;

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

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

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

    // for debugging
    $.pubsubdebug = function() {
    return o;
    };

    })(jQuery);
    })(jQuery);
    9 changes: 0 additions & 9 deletions jquery.ba-tinypubsub.min.js
    Original file line number Diff line number Diff line change
    @@ -1,9 +0,0 @@
    /*
    * 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 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);
  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);