Skip to content

Instantly share code, notes, and snippets.

@prigazzi
Created May 9, 2012 16:46
Show Gist options
  • Save prigazzi/2646543 to your computer and use it in GitHub Desktop.
Save prigazzi/2646543 to your computer and use it in GitHub Desktop.
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