Hi all,
I am not a JQuery expert so I am very confused about creating private variable for an object instance.
I want to create a simple jquery plugin, but all instances see the last declared class variables.
Can someone tell me the reason and the solution ?
Sample code :
;
( function
( $
) {
/* This is a private declaration, isn't it ? */
var settings = {
pagesize: 10
}
/* Constructor */
$.fn.myPlugin =
function
(options
) {
/* Call a private method to save the options */
setVars
( options
);
}
/* Private method to save the options to a PRIVATE variable */
function setVars(options)
{
settings = $.extend
( {}, options
);
}
/* Alert the page size related to the CURRENT instance */
$.fn.myPlugin.refresh =
function ()
{
alert( settings.pagesize
);
}
} ) ( jQuery
);
Calling the plugin :
("
#div1").myPlugin
( {
pagesize : 11
} );
$("
#div2").myPlugin
( {
pagesize : 45
} );
$("#div1").myPlugin.refresh();
$("#div2").myPlugin.refresh();
I think the result should be to alert 11 and 45 but only 45 is alerted two times. But why ?
Thank you in advance
Tihi