What's the right way to store private data in widget's instance?
Sometimes I need some of instance variables to be private, so I can't store them in options. So far I see 2 ways to do this, but both have disadvantages:
First is to store them via .data() with a custom namespace, e. g.
- _create: function(){
- this.element.data("myNS", {
- myVar: "some value"
- })
- }
This way seems pretty comfortable for me, but it is a straight violation of general jqueryUI coding standarts, which say:
Don't save any data on the element other than the plugin instance.
It makes me feel less comfortable about this way, although I don't quite understand the reasons behind this guideline. BTW, jquery.ui.selectable makes it this way.
So the second way is to put our data directly into the plugin instance:
- _create: function(){
- this.myVar = "some value"
- }
This gives us very nice syntax for accessing our properties and is not a violation of coding standards (afaik), but looks like a kind of global pollution. Of course, we can also create a namespace here to avoid pollution and naming clashes, e. g.
- _create: function(){
- this._options = {
- myVar: "some value"
- }
- }
But it just doesn't look right. Or maybe it does?