jQuery UI Extending Widgets via the Widget Factory
jQuery UI's widget factory makes it easier to create widgets that extend the functionality of existing widgets. This lets you build powerful widgets on an existing foundation, or make small adjustments to the functionality of existing widgets.
**Note:** Before studying this chapter, you need to understand what the widget factory is and how it works. If you are not familiar with this, please first check outHow to Use the Widget Factorysection.
Creating a widget extension
Creating a widget via the widget factory is done by passing a $.widget() widget name and a prototype object. The example below creates a "superDialog" widget in the "custom" namespace.
$.widget( "custom.superDialog", {} );
To support extension,$.widget() the widget factory optionally accepts the constructor of a widget to use as a parent. When a parent is specified, pass it as the second argument, after the widget name and before the widget prototype object.
Like the example above, the following also creates a "superDialog" widget in the "custom" namespace. But this time, what is passed is the jQuery UI dialog widget constructor ($.ui.dialog), indicating that the superDialog widget should use the jQuery UI dialog widget as its parent.
$.widget( "custom.superDialog", $.ui.dialog, {} );
Here, the superDialog and dialog widgets are essentially equivalent, differing only in name and namespace. To make our new widget more distinctive, we can add some methods to its prototype object.
The widget's prototype object is the last argument passed to $.widget() . So far, our examples have used an empty object. Now let's add a method to this object:
$.widget( "custom.superDialog", $.ui.dialog, {
red: function() {
this.element.css( "color", "red" );
}
});
// Create a new , convert it into a superDialog, and call the red() method.
$( "I am red" )
.superDialog()
.superDialog( "red" );
Now superDialog There is a red() method that changes its text color to red. Note how the widget factory automatically sets this to the widget's instance object. For a list of all available methods and properties on the instance, visit Widget Factory API Documentation。
Extending existing methods
Sometimes you need to adjust or add behavior to an existing widget method. You can specify the method name on the prototype object to override it. The example below overrides the dialog's open() Method. Since dialogs are open by default, when this code runs,"open" will be logged.
$.widget( "custom.superDialog", $.ui.dialog, {
open: function() {
console.log( "open" );
}
});
// Create a new , and convert it into a superDialog.
$( "" ).superDialog();
When running this code, there is a problem. Since we have overridden the open() default behavior of the method, the dialog no longer appears on screen.
When we use a method on the prototype object, we are actually overriding the original method with a new one in the prototype chain.
To make the parent widget's methods available, the widget factory provides two methods - _super() and _superApply()。
Use _super() and _superApply() to access the parent widget
_super() and _superApply() calls the same method in the parent widget. See the example below. Like the previous example, this one also overrides the open() method to log "open". This time, however, running _super() calls the dialog's open(), and opens the dialog.
$.widget( "custom.superDialog", $.ui.dialog, {
open: function() {
console.log( "open" );
// Invoke the parent widget's open().
return this._super();
}
});
$( "" ).superDialog();
_super() and _superApply() is actually equivalent to the original Function.prototype.call() and Function.prototype.apply() method. Therefore,_super() accepts a list of arguments,_superApply() accepts an array as an argument. The example below demonstrates the difference between the two.
$.widget( "custom.superDialog", $.ui.dialog, {
_setOption: function( key, value ) {
// Both invoke dialog's setOption() method. _super() requires the arguments
// be passed as an argument list, _superApply() as a single array.
this._super( key, value );
this._superApply( arguments );
}
});
Redefining widgets
jQuery UI 1.9 added the ability to redefine widgets. Therefore, instead of creating a new widget, we only need to pass $.widget() an existing widget name and a constructor. The example below adds the same logging to open() , but does so without creating a new widget.
$.widget( "ui.dialog", $.ui.dialog, {
open: function() {
console.log( "open" );
return this._super();
}
});
$( "" ).dialog();
With this approach, we can extend an existing widget method while still being able to use _super() to access the original method - all of this is done without creating a new widget, simply by redefining the widget.
Widgets and Polymorphism
When interacting between widget extensions and their plugins, one thing to note is that the parent widget's plugin cannot be used to call methods on the child widget's elements. The example below demonstrates this.
$.widget( "custom.superDialog", $.ui.dialog, {} );
var dialog = $( "" ).superDialog();
// This works.
dialog.superDialog( "close" );
// This doesn't.
dialog.dialog( "close" );
In the example above, the parent widget's plugin,dialog(), cannot call the close() method on the superDialog element. For more information about calling widget methods, see Widget Method Calls。
Customizing individual instances
So far, the examples we have seen have methods extended on the widget prototype. Methods overridden on the prototype affect all instances of the widget.
To demonstrate this, see the example below. Both dialog instances use the same open() method.
$.widget( "ui.dialog", $.ui.dialog, {
open: function() {
console.log( "open" );
return this._super();
}
});
// Create two dialogs, both use the same open(), therefore "open" is logged twice.
$( "" ).dialog();
$( "" ).dialog();
Sometimes you only need to change the behavior of a particular instance of a widget. To do this, you need to use normal JavaScript property assignment, get a reference to the instance, and override the method. As shown in the example below.
var dialogInstance = $( "" )
.dialog()
// Retrieve the dialog's instance and store it.
.data( "ui-dialog" );
// Override the close() method for this dialog
dialogInstance.close = function() {
console.log( "close" );
};
// Create a second dialog
$( "" ).dialog();
// Select both dialogs and call close() on each of them.
// "close" will only be logged once.
$( ":data(ui-dialog)" ).dialog( "close" );
The technique of overriding methods for individual instances is perfect for one-off customizations.