Basically:
// plugin definition
$.fn.myPlugin = function(options) {
var defaults = {
key1: val1,
key2: val2
};
// Extend our default options with those provided.
var opts = $.extend(defaults, options);
// Our plugin implementation code goes here.
};
To define more functions without cluttering the namespace and without exposing the implementation you shoul wrap your function creating a closure:
// create closure
(function($) {
$.fn.myPlugin = function(options) {
var defaults = {
key1: val1,
key2: val2
};
// Extend our default options with those provided.
var opts = $.extend(defaults, options);
// Our plugin implementation code goes here.
};
// end of closure
})(jQuery);
To provide public access to your function elements (for example options):
// create closure
(function($) {
$.fn.myPlugin = function(options) {
return this.each(function() {
// Extend our default options with those provided.
var opts = $.extend({}, $.fn.myPlugin.defaults , options);
// Our plugin implementation code goes here.
}
};
$.fn.myPlugin.defaults = {
key1: val1,
key2: val2
};
// end of closure
})(jQuery);
Now you can change the defaults before invoking your plugin:
$.fn.myPlugin.defaults.key1= val3;
$("myDOMElements").myPlugin();
Useful links:
answered
Nov 10 '09 at 20:25
Matteo Bicocchi ♦♦
1776●1●2●9