cahnory

jQuery boilerplate

Jun 23rd, 2011
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
jQuery 1.23 KB | None | 0 0
  1. (function($) {
  2.  
  3.     //  Static private vars
  4.     var pluginName = 'boilerplate',
  5.         pStatic = {
  6.             //  Static public vars
  7.             foo: 'bar'
  8.         };
  9.  
  10.     $[pluginName] = function(element, options) {
  11.    
  12.         // Private vars
  13.         var
  14.         plugin      = this,
  15.         $element    = $(element),
  16.         element     = element,
  17.         defaults    = {
  18.             foo: 'bar',
  19.             onFoo: function() {}
  20.         },
  21.        
  22.         // Private methods
  23.         privateMethod = function() {
  24.             // code goes here
  25.         };
  26.        
  27.         // Public methods
  28.         plugin.methods = {
  29.             init: function() {
  30.                 plugin.settings = $.extend({}, defaults, options);
  31.                 // code goes here
  32.             },
  33.             publicMethod: function() {
  34.                 // code goes here
  35.             }
  36.         };
  37.        
  38.         // Public vars
  39.         plugin.settings = {};
  40.         plugin.static   = pStatic;
  41.  
  42.         plugin.methods.init();
  43.  
  44.     }
  45.  
  46.     $.fn[pluginName] = function(options) {
  47.         var args    =   arguments;
  48.         return this.each(function() {
  49.             var i   =   $(this);
  50.             if (undefined == (plugin = i.data(pluginName))) {
  51.                 var plugin = new $[pluginName](this, options);
  52.                 i.data(pluginName, plugin);
  53.             } else if (plugin.methods[options]) {
  54.                 plugin.methods[options].apply( plugin, Array.prototype.slice.call( args, 1 ));
  55.             }
  56.         });
  57.  
  58.     }
  59.  
  60. })(jQuery);
Advertisement
Add Comment
Please, Sign In to add comment