Skip to content

Instantly share code, notes, and snippets.

@marciobarrios
Created March 27, 2009 16:40
Show Gist options
  • Save marciobarrios/86774 to your computer and use it in GitHub Desktop.
Save marciobarrios/86774 to your computer and use it in GitHub Desktop.
jQuery plugin development pattern
//closure
(function($) {
//permitimos acceso externo a las opciones por defecto
$.fn.pluginName.defaults = {
option1: "whatever",
option2: "",
option3: 9,
option4: true
}
//extend jquery with the plugin
$.fn.extend({
pluginName:function(options) {
var options = $.extend({}, $.fn.pluginName.defaults, options); // var options = $.extend($.fn.pluginName.defaults, options);
//return the jquery object for chaining
return this.each(function(){
//aqui el codigo del plugin
privateFunc(options);
});
}
});
//aqui algunas funciones privadas
function privateFunc(options) {
};
//aqui algunas funciones privadas
function privateFunc2() {
}
})(jQuery);
//uso
$('#selector').pluginName({option1:'',option2:''});
//tambien puedo reestablecer las variables por defecto
$.fn.pluginName.defaults = {option1:'',option2:''};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment