Created
March 27, 2009 16:40
-
-
Save marciobarrios/86774 to your computer and use it in GitHub Desktop.
jQuery plugin development pattern
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
//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