Skip to content

Instantly share code, notes, and snippets.

@twinsdbv
Last active December 1, 2016 07:49
Show Gist options
  • Save twinsdbv/4cfbd08b1ff3b76deb92 to your computer and use it in GitHub Desktop.
Save twinsdbv/4cfbd08b1ff3b76deb92 to your computer and use it in GitHub Desktop.
jQuery plugin skeleton
(function( $ ){
var pluginName = 'pluginName';
var self;
// list of methods in your plugin
var methods = {
init : function( options ) {
return self.each(function () {
methods.setSettings(options);
});
},
setSettings: function (options) {
var settings = self.data('settings');
if (! settings) {
self.data('settings', $.extend(methods.getDefaultSettings(), options));
}
},
getSettings: function ($element) {
return $element.data('settings');
},
getDefaultSettings: function () {
return {
// your default settings for plugin
};
}
$.fn[pluginName] = function ( method ) {
self = this;
if ( methods[method] ) {
return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
} else if ( typeof method === 'object' || ! method ) {
return methods.init.apply( this, arguments );
} else {
$.error( 'Method ' + method + ' does not exist in jQuery.pluginName' );
}
}
})( jQuery );
@maplemap
Copy link

maplemap commented Apr 19, 2015

Там затерялась '};' перед $.fn[pluginName] - не закрыт объект 'methods'.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment