Created
May 9, 2012 16:46
-
-
Save prigazzi/2646543 to your computer and use it in GitHub Desktop.
Javascript: Plugin base para jQuery
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;(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