Public variables in jQuery UI widget?
I'm looking for the best way to store a public variable inside a jQuery plugin built on top of the jQuery UI widget factory (jquery.ui.widget.js).
(function($,undefined) {
$.widget('namespace.name',{
publicVar: '',
options: {
},
_create: function() {
console.log(this.publicVar);
},
publicMethod: function() {
},
});
})(jQuery);
The variable need to be unique to each instance of the plugin so it needs to be declared somewhere inside the widget object $.widget('namespace.name',{});
. Furthermore I want it to be accessible from within each method (_create, publicMethod).
So what I tried is declaring the variable 'publicVar' directly inside the widget Object together with the options and methods. This se开发者_JAVA百科ems to work but there is no way to access this variable outside of the widget, or is there?
Any suggestions on how to better define public variables for jQuery widgets are welcomed.
I think your looking for the getter/setter methods that are built into the ui framework. Check the Setters and Getters section of the jQuery UI Developer Guide
look for the part about overriding _SetOption
_setOption: function(key, value) {
// handle new settings and update options hash
}
精彩评论