Advertisement
Guest User

jQuery Pub/Sub plugin v0.2

a guest
Mar 7th, 2011
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* 
  2.     jQuery pub/sub plugin by Túbal Martín (http://margenn.com)
  3.  
  4.     Inspired by Peter Higgings plugin: https://github.com/phiggins42/bloody-jquery-plugins/blob/8ccf0da40b85fa4ecc30079c8650bac58f7a334c/pubsub.js
  5. */
  6.  
  7. (function($) {
  8.    
  9.     // The topics/subscriptions hash
  10.     var topics = {};
  11.    
  12.     /**
  13.      * @description Publishes some data on a named topic.
  14.      * @param {String} topic The topic to publish.
  15.      * @param {Array} [args] The data to publish.          
  16.      * @returns {jQuery} returns jQuery object.
  17.      * @example
  18.      * $( "#sidebarWidget" ).publish( "/some/topic", [ "a", "b", "c" ] );
  19.      */
  20.     $.fn.publish = function ( topic, args ) {  
  21.         args = args || [],
  22.         argsLength = args.length;
  23.        
  24.         if ( topics[topic] ) {
  25.             this.each( function( i, obj ) {
  26.                 // Let's add a reference to the publisher object
  27.                 args[argsLength] = obj;
  28.                 $.each( topics[topic], function( j, aElem ) {
  29.                     aElem.callback.apply( aElem.object, args );
  30.                 });
  31.             });
  32.         }
  33.        
  34.         return this;
  35.     };
  36.    
  37.    
  38.     /**
  39.      * @description Registers a callback on a named topic.
  40.      * @param {String} topic The topic to subscribe to.
  41.      * @param {Function} callback The handler. Anytime something is published on a subscribed
  42.      * topic, the callback will be called with the published array as ordered arguments.           
  43.      * @returns {jQuery} returns jQuery object.
  44.      * @example
  45.      * $( "#header" ).subscribe( "/some/topic", function( a, b, c ) { // handle data } );
  46.      */
  47.     $.fn.subscribe = function( topic, callback ) {
  48.         topics[topic] = topics[topic] || [];
  49.        
  50.         this.each( function( i, obj ) {
  51.             // Array.push() is way slower. See: http://jsperf.com/array-push-el-vs-array-array-length-el
  52.             topics[topic][topics[topic].length] = { "object": obj, "callback": callback };
  53.         });
  54.        
  55.         return this;   
  56.     };
  57.    
  58.    
  59.     /**
  60.      * @description Unregisters a previously registered callback for a named topic.
  61.      * @param {String} topic The topic to unsubscribe from.    
  62.      * @returns {jQuery} returns jQuery object.
  63.      * @example
  64.      * $( "#header" ).unsubscribe( "/some/topic" );
  65.      */
  66.     $.fn.unsubscribe = function ( topic ) {
  67.        
  68.         if ( topics[topic] ) {
  69.             this.each( function( i, obj ) {
  70.                 topics[topic] = $.grep( topics[topic], function( aElem, j ) {
  71.                     return ( obj !== aElem.object );
  72.                 });
  73.             });
  74.            
  75.             if ( topics[topic].length === 0 ) {
  76.                 delete topics[topic];
  77.             }
  78.         }
  79.        
  80.         return this;   
  81.     };
  82.    
  83. }(jQuery));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement