Extending Widgets with the Widget Factory

https://learn.jquery.com/jquery-ui/widget-factory/extending-widgets/

jQuery UI's widget factory makes it easy to build widgets that extend the functionality of existing widgets. Doing so allows you to build powerful widgets on top of an existing base, as well as make small tweaks to an existing widget's functionality.

Note: This article assumes some basic knowledge of what the widget factory is and how it works. If you're unfamiliar with this, read up on how to use the widget factory first.

link Creating Widget Extensions

Creating widgets with the widget factory is done by passing the name of the widget and a prototype object to $.widget().

The following creates a "superDialog" widget in the "custom" namespace.

$.widget( "custom.superDialog", {} );

To allow for extension, $.widget() optionally accepts the constructor of a widget to use as a parent.

When specifying a parent widget, pass it as the second argument - after the widget's name, and before the widget's prototype object.

Like the previous example, the following also creates a "superDialog" widget in the "custom" namespace.

However, this time the constructor of jQuery UI's dialog widget ($.ui.dialog) is passed, indicating that the superDialog widget should use jQuery UI's dialog widget as a parent.

$.widget( "custom.superDialog", $.ui.dialog, {} );

Here superDialog and dialog are essentially equivalent widgets with different names and namespaces. To make our new widget more interesting we can add methods to its prototype object.

A widget's prototype object is the final argument passed to $.widget(). So far, our examples have been using an empty object. Let's add a method to this object:

$.widget( "custom.superDialog", $.ui.dialog, {
    red: function() {
        this.element.css( "color", "red" );
    }
});
 
// Create a new <div>, convert it into a superDialog, and call the red() method.
$( "<div>I am red</div>" )
    .superDialog()
    .superDialog( "red" );

Now the superDialog has a red() method that will change the color of its text to red.

Note how the widget factory automatically sets this to the widget's instance object.

For a full list of the methods and properties available on the instance, see the widget factory's API documentation.

Extending Existing Methods

Sometimes you need to tweak or add to the behavior of existing widget methods. The do this, specify a method with the same name as the method you want to override on the prototype object. The following example overrides dialog's open() method. Since dialogs automatically open by default, "open" will be logged when this code runs.

1
2
3
4
5
6
7
8
$.widget( "custom.superDialog", $.ui.dialog, {
open: function() {
console.log( "open" );
}
});
 
// Create a new <div>, and convert it into a superDialog.
$( "<div>" ).superDialog();

While this runs, there's a problem. Since we overrode the default behavior of open(), the dialog no longer displays on the screen.

When we place methods on the prototype object, we are not actually overriding the original method - rather, we are placing a new method at a higher level in the prototype chain.

To make the parent's methods available, the widget factory provides two methods - _super() and _superApply().

Using _super() and _superApply() to Access Parents

_super() and _superApply() invoke methods of the same name in the parent widget. Refer to the following example. Like the previous one, this example also overrides the open() method to log "open". However, this time _super() is run to invoke dialog's open() and open the dialog.

1
2
3
4
5
6
7
8
9
10
$.widget( "custom.superDialog", $.ui.dialog, {
open: function() {
console.log( "open" );
 
// Invoke the parent widget's open().
return this._super();
}
});
 
$( "<div>" ).superDialog();

_super() and _superApply() were designed to behave like the native Function.prototype.call() and Function.prototype.apply() methods. Therefore, _super() accepts an argument list, and _superApply() accepts a single array of arguments. This difference is shown in the example below.

1
2
3
4
5
6
7
8
9
$.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 );
}
});

猜你喜欢

转载自www.cnblogs.com/chucklu/p/11078704.html