Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Javascript: Plugin base para jQuery
;(function( $, window, document, undefined) {
var pluginName = 'ejemplo',
defaults = {
autoExec : true,
autoExecMethod : 'auto',
default : 'values',
}
// Código de inicialización acá
methods = {
'init' : function(options) {
settings = $.extend(true, {}, defaults, options);
this.each(function() {
var $this = $(this),
data = $this.data(pluginName);
if( data != undefined ) {
$.extend(settings, data);
}
$this.data(pluginName, {
"settings" : settings,
});
});
return settings.autoExec ? methods[settings.autoExecMethod].call(this) : this;
},
'auto' : function() {
return this.each(function() {
var $target = $(this),
settings = $target.data(pluginName).settings;
console.log($target, settings);
});
},
}
$.fn[pluginName] = function(method) {
if (typeof method == "object" || !method) {
arg = [method];
method = 'init';
} else if (methods[method]) {
arg = Array.prototype.slice.call(arguments, 1);
} else {
$.error('El método ' + method + ' invocado, no existe');
}
return methods[method].apply(this, arg);
}
})(jQuery, window, document)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.