Advertisement
Guest User

jQuery plugin boilerplate

a guest
Jan 16th, 2013
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function($){
  2.  
  3.     var oConfig = {
  4.  
  5.     };
  6.     var sDataName = '';
  7.     var sPluginName = '';
  8.  
  9.     var PluginMethods  = {
  10.  
  11.         /**
  12.          * initializing plugin
  13.          * @param oOptions
  14.          */
  15.         init : function( oOptions ) {
  16.             var oCurrentConfig = {};
  17.         // if it's already inited, take current config
  18.             if ( $(this).data( sDataName ) ) {
  19.                 oCurrentConfig = $(this).data( sDataName ).oOptions;
  20.             }
  21.         // overriding config params
  22.             $.extend( oCurrentConfig, oConfig, oOptions || {} );
  23.             if ( !$(this).data( sDataName ) ) {
  24.                 $(this).data( sDataName , new Plugin( $(this), oCurrentConfig ) );
  25.             }
  26.             $(this).data( sDataName ).init( oOptions );
  27.  
  28.         },
  29.  
  30.         destroy : function() {
  31.             if ( $(this).data( sDataName ) ) {
  32.                 $(this).data( sDataName ).destroy();
  33.             }
  34.         },
  35.  
  36.         enable : function() {
  37.             if ( $(this).data( sDataName ) ) {
  38.                 $(this).data( sDataName ).enable();
  39.             }
  40.         },
  41.  
  42.         disable : function() {
  43.             if ( $(this).data( sDataName ) ) {
  44.                 $(this).data( sDataName ).disable();
  45.             }
  46.         }
  47.     };
  48.  
  49.  
  50.     var Plugin = function( $Node, oOptions ) {
  51.  
  52.         this.$Container = $Node;
  53.  
  54.         this.init = function() {
  55.  
  56.         };
  57.  
  58.         this.destroy = function () {};
  59.         this.enable = function () {};
  60.         this.disable = function () {};
  61.  
  62.     };
  63.  
  64.     $.fn[sPluginName] = function( /** arguments: method, options | options **/) {
  65.         // default settings
  66.         var sMethod = 'init', oOptions = {};
  67.         // have a single param or nothing at all
  68.         if ( arguments.length < 2 ) {
  69.             // if it's a single param and it's a string -> it's a method name
  70.             if ( arguments.length && typeof arguments[0] == 'string' ) {
  71.                 sMethod = arguments[0];
  72.                 // else if it's a single param and an object -> it's init options
  73.             } else {
  74.                 oOptions = arguments[0];
  75.             }
  76.             // if number of params is 2 or greater
  77.         } else if ( typeof arguments[0] == 'string' ) {
  78.             // first param is a method name
  79.             sMethod = arguments[0];
  80.             // if second param is an object, consider it as an options
  81.             if ( typeof arguments[1] == 'object' ) {
  82.                 oOptions = arguments[1];
  83.             }
  84.         }
  85.         // if method exists, run it with computed options
  86.         if ( PluginMethods.hasOwnProperty( sMethod ) && typeof PluginMethods[ sMethod ] == 'function' ) {
  87.             return this.each( function() {
  88.                 return PluginMethods[ sMethod ].apply( this, [ oOptions ] );
  89.             });
  90.         } else {
  91.             return this;
  92.         }
  93.     }
  94. })(jQuery);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement