I am trying to write an extension to jQuery, whereby you can pass methods by strings, and apply these methods to the element specified. However, I cannot understand how calling a method this way can determine the options that were originally set when the method was called.
Consider this (using jQuery UI as example):
$('#foo').datepicker({ someoption: true });
$('#foo').datepicker('setDate','10/10/2013');
How does the second call know what someoption
is set too?
If, for instance, the someoption
was dateFormat
then the datepicker would automatically change the format of the supplied date to match the format specified in formatDate
... How does the second call know what the value of formatDate
is?
The only way I can see how this is achieved is like so (obviously missing a few details):
var datepickers = new Array();
$.fn.datepicker = datepicker;
function datepicker(options){
// Get the selector somehow
var selector = somefunction($(this));
// Save the options to the datepickers array
// for the given selector
datepickers[selector] = options;
}
Is this the correct way to go about achieving this? If so, how does one get the selector and is it reliable (potential solution to this part here)?