I am new to jQuery and especially to JavaScript.
I am trying to create a jQuery plugin but have some difficulties with callback events. I want to include callback events into different functions, so that the plugin will be a little bit flexible.
Here is the code:
(function($, undefined){
var methods = {
init: function(options){
var settings = $.extend({
// Default settings
'fadeInOutSpeed' : 100,
// the rest of default settings ...
// Callback events
onOpenBefore : function(){},
onOpen : function(){},
onOpenedCloseBefore : function(){}
// the rest of the callback events...
}, options);
// code goes here...
return this.each(function(){
// code goes here
});
},
destroy: function(){
// code goes here
},
open: function(){
console.info('method open accessed');
},
close: function(){
console.info('method close accessed');
},
update: function(){
console.info('method update accessed');
}
};
function gui_selectbox_open(param, event){
// HOW TO PUT A CORRECT CALLBACK CODE HERE ?
settings.onOpenBefore.call(param, event);
// function code goes here
settings.onOpenAfter.call(param, event);
};
Inside the function gui_selectbox_open(param,event), there is a line at the beginning, where I want to put a callback event, so I could access it properly when initializing a plugin from outside, which gives me an JavaScript error, telling that settings.onOpenBefore... is not defined. How to define it?
Thanks in advance
Your variable
settingsis defined as local to theinit()function inside yourmethodsobject. to make it accessible outside, you can define it outside theinitfunction and access it asmethods.settings.yourMethod(..)i.e.