Hello,
What is the best way to create a static variables for jQuery plugins?
I have 2 example use cases to illustrate my thinking so far; each with some ideas.
Any other ideas welcomed of course...
One example is for a static variable containing: animation settings, layout settings, product details, etc; the other for a static variable caching data.
I hope 'static' is the correct terminolgy here... single globals. please correct if wrong.
Case 1: for settings
// defining globally
var anObject = {
value1 = 0;
value2 = 0;
}
jQuery.anObjectSet(partialObject) {
anObject = jQuery.extend(anObject, partialObject);
}
jQuery.fn.myPlugin = function (partialObject) {
obj = jQuery.extend(anObject, partialObject);
}
or maybe?
// adding to the jQuery object
jQuery.anObject = {
value1 = 0;
value2 = 0;
}
jQuery.anObjectSet(partialObject) {
jQuery.anObject = jQuery.extend(jQuery.anObject, partialObject);
}
jQuery.fn.myPlugin = function (partialObject) {
obj = jQuery.extend(jQuery.anObject, partialObject);
}
or maybe?
jQuery.anObjectSet(partialObject) {
if(!jQuery.anObject)
jQuery.anObject = {
value1 = 0;
value2 = 0;
}
jQuery.anObject = jQuery.extend(jQuery.anObject, partialObject);
}
jQuery.fn.myPlugin = function (partialObject) {
if(!jQuery.anObject)
jQuery.anObject = {
value1 = 0;
value2 = 0;
}
obj = jQuery.extend(jQuery.anObject, partialObject);
}
Case 2: for caching
jQuery.fn.myPlugin = function (newObject) {
if(!cache[newObject])
cache[newObject] = $(newObject);
return cache[newObject];
}
or maybe? (I seen this method elsewhere)
window.$cache = {};
jQuery.fn.myPlugin = function (newObject) {
if(!$cache[newObject])
$cache[newObject] = $(newObject);
return $cache[newObject];
}
Thanks. I want to build up a .js library, getting starting on the right track...