JQuery provides an extensive reusability system in the form of plugins and widgets. Plugins are native to JQuery and reside as a function that can be defined either statically, or per a range of elements. We’ll touch on JQuery plugins in a later post. JQuery widgets, on the other hand, are a part of the JQuery UI framework. At a minimum, from the JQuery UI download page (http://jqueryui.com/download/), all you need to download is the core feature, the widget feature, and the mouse feature (for mouse-oriented widgets).
This now allows us to create a widget as so:
JQuery begins a widget by using the $.widget method (added on by JQuery UI). This is the point the widget gets defined; the definition of the widget comes in the form of a class definition, as we’ll see soon. It may be beneficial to see how you would create a widget, to add some perspective. Below is how we would instantiate our HelpBox widget.
Here you can see a widget is applied to an element. A widget is linked to an element within the DOM. It is at this point we are actually instantiating our HelpBox widget, and providing the initial settings for the widget (or options).
Jumping back to our widget definition, when its created, the _create and _init methods are invoked. In most scenarios, the _init method is sufficient. Below is the body of the widget defined in the “Definition of Widget” section in the comment above, in addition to some other methods.
During init, we add instance variables to the widget using “this.variable”, which appends a variable to the widget instance. The “this” pointer points to an instance of the widget, which also allows us to call additional method. Notice the use of the extend method? This JQuery method is pretty handy; what it does is compare the first object against the second, and anywhere a member doesn’t exist in the second object, the default value provided in the first object is added to it, and returned from the method. So you can see the first object that’s statically defined is ensuring certain options have been defined by the user. This way, they won’t cause undefined errors later on.
Let’s turn to constructing the user interface:
At the core of each widget is an element. This element is referred to by “this.element”, a reference to the JQuery object containing that element. Here we are creating some DIVs and other elements, applying CSS to the elements, which will be the user interface of the widget. The user interface has a button, which listens to the click event. On click of the button, it invokes a next method, a method that performs an action on the widget. We’ll look at that in the next post. Notice how we store a reference to each element of the widget as a variable reference (self is a pointer to this, this widget).
Usages of Widgets
Using widgets is a little awkward. What looks like the same syntax is actually not the same. Let’s look at how we may initialize the widget again:
This is the point of initialization. This calls _create and _init. We provided some options, which override the defaults that we specified in the _init method (“Okay” overrides “OK” as the default button text). Now, to call a method (like the run method) using the JQuery widget, the following is the syntax.
We don’t actually call a method on the object; we use the widget instance to invoke the method. It’s a little awkward at first, but eventually you get used to it. To call our add method, with parameters, you simply pass in the parameters like so:
You can see that all interaction with the widget goes through the widget, rather than the widget’s API. You can also override the options by first specifying the “options” collection, then the name of the option, and lastly provide the new value.
I hope this was an informative look at using JQuery Widgets in your applications.