Hello,
I'm writing my first jQuery plugin and encouter some difficulties. Ik have my methods which are available to the public. Beside the public methods i have some private functions.
The plugin is chainable and each element can have his own settings. But beside that i want the plugin to have some 'global' settings. The problem is i can't get to these setting variables from whitin the private functions.
example:
- (function($)
{
// define your methods here.
var methods = {
init : function (options) {
var settings = {
propertyA: 'window'
};
return this.each(function(){
// Each element will get it's own settings
var settings = {
propertyA : 'whatever',
propertyB : true
};
// if options exists, lets merge them
// with our default settings
if ( options ) {
$.extend(settings, options);
}
// Declare plugin and $plugin since callback functions will
// have their own "this"
var plugin = this;
var $plugin = $(this);
/*
plugin code goes here...
*/
$plugin.bind('click.pluginName', function(){
doSomething($(this));
});
});
},
show : function (){},
hide : function (){},
update: function(){}
};
$.fn.pluginName = function( method )
{
// Method calling logic
if (methods[method]) {
return methods [method].apply(this, Array.prototype.slice.call(arguments, 1));
}else if ( typeof method === 'object' || ! method ) {
return methods.init.apply(this, arguments);
}else{
$.error( 'Method '+ method +' does not exists on jQuery.pluginName' );
}
};
/*
* PRIVATE FUNCTIOSN
*/
function doSomething($obj){
var $objectsettings = $obj.settings; // I want here the object settings
var $pluginsettings = this.settings; // I want here the plugin settings which can be default
}
})(jQuery);
usage:
- $('.mclass').pluginName({message:'hello'});
question:
How can i call the element settings and the plugin settings from within a private function?