Getting and setting values for jQuery widgets
December 11th, 2009If you come from an OOP background you’re likely used to defining class variables like this:
class MyClass { var internalValue1; var internalValue2; function constructor() { … } }
The values which are to be stored in your object are simple, straightforward, likely to be typed, and the compiler will detect typos in the variable names. JavaScript, as a loosely types language which treats its objects more like collections than discrete objects, can’t really offer us those protections. Conversely, that also means that we have a great deal of flexibility.
In essence, we can dynamically subclass our objects, which you have to admit is a neat trick.
So, for jQuery.UI widgets, the variables stored within our widget are not defined until we assign them. The act of assignment defines the variable name, which can then be called upon at will.
Assignment is simply:
this._setData(‘variablename’, variablevalue);
Retrieval of this value is similarly straightforward:
this._getData(‘variablename’);
This does mean that the old problem of misnamed variables is alive and well. It also means that you need to be prepared to handle an undefined response to the _getData call.


Previous Post
Next Post

4 Comments
March 29th, 2010 at 1:24 am
Hey there,
We have been reading the articles on your website http://www.ajaxbestiary.com/
and are very impressed with the quality of your information.
We have a team of copywriters who specialize in writing articles on various topics and would like to write an original article for you to use on your website – this article will not be used anywhere else on the Internet.
In exchange all we ask is that we can have one or two links within the body of the article back to one of our sites. You can view a sample of the quality of our articles at http://www.prowebsitetemplates.com/_blog/Articles
If you are interested in having us write an article for your website please just let me know and we would be more than happy to have one written for you within two weeks.
Kind regards,
Papia
April 1st, 2010 at 1:05 am
Awesome article!
April 7th, 2010 at 3:03 am
nice and simple for the beginners! like it. definitely keep me reading everyday and easy then to pick it up step by step. instead of being put off by piles of code. nice one!
July 21st, 2010 at 9:30 am
FlexBox is a jQuery plugin that is intended to be a very flexible replacement for html textboxes and dropdowns, using ajax to retrieve and bind JSON data.
Leave a Reply