Regarding the jquery ui widget factory...
What is the best way to have a static variable/class level variable that is shared amongst all instances?
e.g.
$.widget("ui.staticTest", { staticVar:'unchanged', test: function(a){ if(a){ this.staticVar= a; } alert(JSON.stringify(this.staticVar)); } }); $('#test').staticTest(); $('#parent').staticTest(); $('#test').staticTest('test','changed'); $('#parent').staticTest('test');
in the above, if staticVar were static, $('#parent').staticTest('test'); would alert 'changed' but instead it alerts 'unchanged'.
(this code is on jsfiddle if you wanna have a play: http://jsfiddle.net/alzclarke/Sx8pJ/)
The solutions i can think of myself are ugly:
1) $('body').data('sharedVariable', myData) - this doesn't seem like good practice, what if someone or something clears the body of data 2) store it in the prototype namespace e.g. = $.ui.staticTest.prototype.staticVar = myData; this also rings alarm bells