|
| 1 | +--- |
| 2 | +title: Extending Widgets with the Widget Factory |
| 3 | +level: advanced |
| 4 | +--- |
| 5 | + |
| 6 | +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. |
| 7 | + |
| 8 | +**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](/jquery-ui/widget-factory/how-to-use-the-widget-factory/) first. |
| 9 | + |
| 10 | +### Creating Widget Extensions |
| 11 | + |
| 12 | +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. |
| 13 | + |
| 14 | +``` |
| 15 | +$.widget( "custom.superDialog", {} ); |
| 16 | +``` |
| 17 | + |
| 18 | +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. |
| 19 | + |
| 20 | +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](http://jqueryui.com/dialog/) (`$.ui.dialog`) is passed, indicating that the superDialog widget should use jQuery UI's dialog widget as a parent. |
| 21 | + |
| 22 | +``` |
| 23 | +$.widget( "custom.superDialog", $.ui.dialog, {} ); |
| 24 | +``` |
| 25 | + |
| 26 | +Here superDialog and dialog are essentially equivalent widgets with different names and namepaces. To make our new widget more interesting we can add methods to its prototype object. |
| 27 | + |
| 28 | +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: |
| 29 | + |
| 30 | +``` |
| 31 | +$.widget( "custom.superDialog", $.ui.dialog, { |
| 32 | + red: function() { |
| 33 | + this.element.css( "color", "red" ); |
| 34 | + } |
| 35 | +}); |
| 36 | +
|
| 37 | +// Create a new <div>, convert it into a superDialog, and call the red() method. |
| 38 | +$( "<div>I am red</div>" ) |
| 39 | + .superDialog() |
| 40 | + .superDialog( "red" ); |
| 41 | +``` |
| 42 | + |
| 43 | +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](http://api.jqueryui.com/jquery.widget/). |
| 44 | + |
| 45 | +### Extending Existing Methods |
| 46 | + |
| 47 | +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](http://api.jqueryui.com/dialog/#method-open). Since dialogs automatically open by default, `"open"` will be logged when this code runs. |
| 48 | + |
| 49 | +``` |
| 50 | +$.widget( "custom.superDialog", $.ui.dialog, { |
| 51 | + open: function() { |
| 52 | + console.log( "open" ); |
| 53 | + } |
| 54 | +}); |
| 55 | +
|
| 56 | +// Create a new <div>, and convert it into a superDialog. |
| 57 | +$( "<div>" ).superDialog(); |
| 58 | +``` |
| 59 | + |
| 60 | +While this runs, there's a problem. Since we overrode the default behavior of `open()`, the dialog no longer displays on the screen. |
| 61 | + |
| 62 | +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. |
| 63 | + |
| 64 | +To make the parent's methods available, the widget factory provides two methods - `_super()` and `_superApply()`. |
| 65 | + |
| 66 | +### Using `_super()` and `_superApply()` to Access Parents |
| 67 | + |
| 68 | +[`_super()`](http://api.jqueryui.com/jquery.widget/#method-_super) and [`_superApply()`](http://api.jqueryui.com/jquery.widget/#method-_superApply) invoke methods of the same same 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. |
| 69 | + |
| 70 | +``` |
| 71 | +$.widget( "custom.superDialog", $.ui.dialog, { |
| 72 | + open: function() { |
| 73 | + console.log( "open" ); |
| 74 | +
|
| 75 | + // Invoke the parent widget's open(). |
| 76 | + return this._super(); |
| 77 | + } |
| 78 | +}); |
| 79 | +
|
| 80 | +$( "<div>" ).superDialog(); |
| 81 | +``` |
| 82 | + |
| 83 | +`_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. |
| 84 | + |
| 85 | +``` |
| 86 | +$.widget( "custom.superDialog", $.ui.dialog, { |
| 87 | + _setOption: function( key, value ) { |
| 88 | +
|
| 89 | + // Both invoke dialog's setOption() method. _super() requires the arguments |
| 90 | + // be passed as an argument list, _superApply() as a single array. |
| 91 | + this._super( key, value ); |
| 92 | + this._superApply( arguments ); |
| 93 | + } |
| 94 | +}); |
| 95 | +``` |
| 96 | + |
| 97 | +### Redefining Widgets |
| 98 | + |
| 99 | +jQuery UI 1.9 added the ability for widgets to redefine themselves. Therefore, instead of creating a new widget, we can pass `$.widget()` an existing widget's name and constructor. The following example adds the same logging in `open()`, but doesn't create a new widget to do so. |
| 100 | + |
| 101 | +``` |
| 102 | +$.widget( "ui.dialog", $.ui.dialog, { |
| 103 | + open: function() { |
| 104 | + console.log( "open" ); |
| 105 | + return this._super(); |
| 106 | + } |
| 107 | +}); |
| 108 | +
|
| 109 | +$( "<div>" ).dialog(); |
| 110 | +``` |
| 111 | + |
| 112 | +With this approach you can extend an existing widget's method and still have access to the original methods using `_super()` - all without creating a new widget. |
| 113 | + |
| 114 | +### Widgets and Polymorphism |
| 115 | + |
| 116 | +One word of warning when interacting with widget extensions and their plugins. The parent widget's plugin cannot be used to invoke methods on elements that are child widgets. This is shown in the example below. |
| 117 | + |
| 118 | +``` |
| 119 | +$.widget( "custom.superDialog", $.ui.dialog, {} ); |
| 120 | +
|
| 121 | +var dialog = $( "<div>" ).superDialog(); |
| 122 | +
|
| 123 | +// This works. |
| 124 | +dialog.superDialog( "close" ); |
| 125 | +
|
| 126 | +// This doesn't. |
| 127 | +dialog.dialog( "close" ); |
| 128 | +``` |
| 129 | + |
| 130 | +Above, the parent widget's plugin, `dialog()`, cannot invoke the `close()` method on an element that is a superDialog. For more on the invoking widget methods see [Widget Method Invocation](/jquery-ui/widget-factory/widget-method-invocation/). |
| 131 | + |
| 132 | +### Customizing Individual Instances |
| 133 | + |
| 134 | +All the examples we have looked at so far have extended methods on the widget's prototype. Methods overridden on the prototype affect all instances of the widget. |
| 135 | + |
| 136 | +To show this, refer to the example below; both instances of the dialog use the same `open()` method. |
| 137 | + |
| 138 | +``` |
| 139 | +$.widget( "ui.dialog", $.ui.dialog, { |
| 140 | + open: function() { |
| 141 | + console.log( "open" ); |
| 142 | + return this._super(); |
| 143 | + } |
| 144 | +}); |
| 145 | +
|
| 146 | +// Create two dialogs, both use the same open(), therefore "open" is logged twice. |
| 147 | +$( "<div>" ).dialog(); |
| 148 | +$( "<div>" ).dialog(); |
| 149 | +``` |
| 150 | + |
| 151 | +While this is powerful, sometimes you only need to change the behavior for a single instance of the widget. To do this, obtain a reference to the instance and override the method using normal JavaScript property assignment. The example below shows this. |
| 152 | + |
| 153 | +``` |
| 154 | +var dialogInstance = $( "<div>" ) |
| 155 | + .dialog() |
| 156 | + // Retrieve the dialog's instance and store it. |
| 157 | + .data( "ui-dialog" ); |
| 158 | +
|
| 159 | +// Override the close() method for this dialog |
| 160 | +dialogInstance.close = function() { |
| 161 | + console.log( "close" ); |
| 162 | +}; |
| 163 | +
|
| 164 | +// Create a second dialog |
| 165 | +$( "<div>" ).dialog(); |
| 166 | +
|
| 167 | +// Select both dialogs and call close() on each of them. |
| 168 | +// "close" will only be logged once. |
| 169 | +$( ":data(ui-dialog)" ).dialog( "close" ); |
| 170 | +``` |
| 171 | + |
| 172 | +This technique of overriding methods for individual instances is perfect for one-off customizations. |
0 commit comments