Advertisement
peremenov

jQuery plugin template

Dec 12th, 2012
3,106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. Usage:
  3.     $('selector').pluginname();
  4.     $('selector').pluginname('update', { txt : 'text' });
  5.     $('selector').pluginname('destroy');
  6. */
  7. ;
  8. (function($) {
  9.  
  10.     var __pluginName = 'pluginname';
  11.  
  12.     var Obj = function($self, opt) {
  13.         this.$self = $self;
  14.         this.opt = $.extend({
  15.            
  16.         }, opt);
  17.  
  18.         this.init();
  19.     };
  20.  
  21.     Obj.prototype = {
  22.         init : function() {
  23.            
  24.         },
  25.         destroy : function() {
  26.             this.$self.removeData(__pluginName);
  27.         },
  28.         update : function(opt) {
  29.             this.opt = $.extend(this.opt, opt);
  30.         }
  31.     };
  32.  
  33.     $.fn[__pluginName] = function(opt, param) {
  34.         return this.each(function() {
  35.             var $self = $(this);
  36.             var obj;
  37.             if(!!(obj = $self.data(__pluginName))) {
  38.                 if(typeof opt === 'string' && !!obj[opt])
  39.                     obj[opt](param);
  40.                 else
  41.                     obj.update(opt);
  42.             } else {
  43.                 $self.data(__pluginName, new Obj($self, opt));
  44.             }
  45.         });
  46.     }
  47. })(jQuery);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement