Contents:
jQuery.widget( name [, base ], prototype )
Description: Create stateful jQuery plugins using the same abstraction as all jQuery UI widgets.
-
jQuery.widget( name [, base ], prototype )
-
nameType: StringThe name of the widget to create, including the namespace.
-
baseType: Function()The base widget to inherit from. This must be a constructor that can be instantiated with the `new` keyword. Defaults to
jQuery.Widget. -
prototypeType: PlainObjectThe object to use as a prototype for the widget.
-
You can create new widgets from scratch, using just the $.Widget object as a base to inherit from, or you can explicitly inherit from existing jQuery UI or third-party widgets. Defining a widget with the same name as you inherit from even allows you to extend widgets in place.
jQuery UI contains many widgets that maintain state and therefore have a slightly different usage pattern than typical jQuery plugins. All of jQuery UI's widgets use the same patterns, which is defined by the widget factory. So if you learn how to use one widget, then you'll know how to use all of them.
Note: This documentation shows examples using the progressbar widget but the syntax is the same for every widget.
Initialization
In order to track the state of the widget, we must introduce a full life cycle for the widget. The life cycle starts when the widget is initialized. To initialize a widget, we simply call the plugin on one or more elements.
|
1
|
|
This will initialize each element in the jQuery object, in this case the element with an id of "elem".
Options
Because progressbar() was called with no parameters, the widget was initialized with its default options. We can pass a set of options during initialization to override the defaults:
|
1
|
|
We can pass as many or as few options as we want during initialization. Any options that we don't pass will just use their default values.
The default values are stored on the widget's prototype, therefore we have the ability to override the values that jQuery UI sets. For example, after setting the following, all future progressbar instances will default to a value of 80:
|
1
|
|
The options are part of the widget's state, so we can set options after initialization as well. We'll see this later with the option method.
Methods
Now that the widget is initialized, we can query its state or perform actions on the widget. All actions after initialization take the form of a method call. To call a method on a widget, we pass the name of the method to the jQuery plugin. For example, to call the value() method on our progressbar widget, we would use:
|
1
|
|
If the method accepts parameters, we can pass them after the method name. For example, to pass the parameter 40 to the value() method, we can use:
|
1
|
|
Just like other methods in jQuery, most widget methods return the jQuery object for chaining.
|
1
2
3
|
|
Each widget will have its own set of methods based on the functionality that the widget provides. However, there are a few methods that exist on all widgets, which are documented below.
Events
All widgets have events associated with their various behaviors to notify you when the state is changing. For most widgets, when the events are triggered, the names are prefixed with the widget name. For example, we can bind to progressbar's change event which is triggered whenever the value changes.
|
1
2
3
|
|
Each event has a corresponding callback, which is exposed as an option. We can hook into progressbar's change callback instead of binding to the progressbarchange event, if we want to.
|
1
2
3
4
5
|
|
All widgets have a create event which is triggered upon instantiation.
Instance
The widget's instance is stored using jQuery.data() with the widget's full name as the key. Therefore, you can use the following to retrieve the progressbar widget's instance object from the element.
|
1
|
|
Whether an element has a given widget bound to it can be determined using the :data selector.
|
1
2
|
|
You can also use :data to get a list of all elements that are instances of a given widget.
|
1
|
|
Properties
All widgets have the following set of properties:
-
document: The
documentthat the widget's element is within. Useful if you need to interact with widgets within iframes. -
element: A jQuery object containing the element used to instantiate the widget. If you select multiple elements and call
.myWidget(), a separate widget instance will be created for each element. Therefore, this property will always contain one element. -
namespace: The location on the global jQuery object that the widget's prototype is stored on. For example a
namespaceof"ui"indicates that the widget's prototype is stored on$.ui. -
options: An object containing the options currently being used by the widget. On instantiation, any options provided by the user will automatically be merged with any default values defined in
$.myNamespace.myWidget.prototype.options. User specified options override the defaults. - uuid: A unique integer identifier for the widget.
- version: The string version of the widget. For jQuery UI widgets this will be set to the version of jQuery UI the widget is using. Widget developers have to set this property in their prototype explicitly.
-
widgetEventPrefix: The prefix prepended to the name of events fired from this widget. For example the
widgetEventPrefixof the draggable widget is"drag", therefore when a draggable is created, the name of the event fired is"dragcreate". By default thewidgetEventPrefixof a widget is its name. Note: This property is deprecated and will be removed in a later release. Event names will be changed to widgetName:eventName (e.g."draggable:create". -
widgetFullName: The full name of the widget including the namespace. For
$.widget( "myNamespace.myWidget", {} ),widgetFullNamewill be"myNamespace-myWidget". -
widgetName: The name of the widget. For
$.widget( "myNamespace.myWidget", {} ),widgetNamewill be"myWidget". -
window: The
windowthat the widget's element is within. Useful if you need to interact with widgets within iframes.
Base Widget
Description: The base widget used by the widget factory.
Options
disabledType: Boolean
false
true.Initialize the widget with the disabled option specified:
|
1
|
|
Get or set the disabled option, after initialization:
|
1
2
3
4
5
6
|
|
hideType: Boolean or Number or String or Object
null
-
Boolean:
When set to
false, no animation will be used and the element will be hidden immediately. When set totrue, the element will fade out with the default duration and the default easing. - Number: The element will fade out with the specified duration and the default easing.
-
String:
The element will be hidden using the specified effect.
The value can either be the name of a built-in jQuery animation method, such as
"slideUp", or the name of a jQuery UI effect, such as"fold". In either case the effect will be used with the default duration and the default easing. -
Object: If the value is an object, then
effect,delay,duration, andeasingproperties may be provided. If theeffectproperty contains the name of a jQuery method, then that method will be used; otherwise it is assumed to be the name of a jQuery UI effect. When using a jQuery UI effect that supports additional settings, you may include those settings in the object and they will be passed to the effect. Ifdurationoreasingis omitted, then the default values will be used. Ifeffectis omitted, then"fadeOut"will be used. Ifdelayis omitted, then no delay is used.
Initialize the widget with the hide option specified:
|
1
|
|
Get or set the hide option, after initialization:
|
1
2
3
4
5
6
|
|
showType: Boolean or Number or String or Object
null
-
Boolean:
When set to
false, no animation will be used and the element will be shown immediately. When set totrue, the element will fade in with the default duration and the default easing. - Number: The element will fade in with the specified duration and the default easing.
-
String:
The element will be shown using the specified effect.
The value can either be the name of a built-in jQuery animation method, such as
"slideDown", or the name of a jQuery UI effect, such as"fold". In either case the effect will be used with the default duration and the default easing. -
Object: If the value is an object, then
effect,delay,duration, andeasingproperties may be provided. If theeffectproperty contains the name of a jQuery method, then that method will be used; otherwise it is assumed to be the name of a jQuery UI effect. When using a jQuery UI effect that supports additional settings, you may include those settings in the object and they will be passed to the effect. Ifdurationoreasingis omitted, then the default values will be used. Ifeffectis omitted, then"fadeIn"will be used. Ifdelayis omitted, then no delay is used.
Initialize the widget with the show option specified:
|
1
|
|
Get or set the show option, after initialization:
|
1
2
3
4
5
6
|
|
Methods
_create()
_create() method is the widget's constructor.
There are no parameters, but this.element and this.options are already set.
- This method does not accept any arguments.
_delay( fn [, delay ] )Returns: Number
this context correct. Essentially setTimeout().
Returns the timeout ID for use with clearTimeout().
-
fnThe function to invoke. Can also be the name of a method on the widget.
-
delayType: NumberThe number of milliseconds to wait before invoking the function. Defaults to
0.
_destroy()
destroy() method cleans up all common data, events, etc. and then delegates out to _destroy() for custom, widget-specific, cleanup.
- This method does not accept any arguments.
_focusable( element )
element to apply the ui-state-focus class on focus.
The event handlers are automatically cleaned up on destroy.
-
elementType: jQueryThe element(s) to apply the focusable behavior to.
_getCreateOptions()Returns: Object
- This method does not accept any arguments.
_hide( element, option [, callback ] )
option values.
_hoverable( element )
element to apply the ui-state-hover class on hover.
The event handlers are automatically cleaned up on destroy.
-
elementType: jQueryThe element(s) to apply the hoverable behavior to.
_init()
Note: Initialization should only be handled if there is a logical action to perform on successive calls to the widget with no arguments.
- This method does not accept any arguments.
_on( [suppressDisabledCheck ] [, element ], handlers )
click .foo". The _on() method provides several benefits of direct event binding:
- Maintains proper
thiscontext inside the handlers. - Automatically handles disabled widgets: If the widget is disabled or the event occurs on an element with the
ui-state-disabledclass, the event handler is not invoked. Can be overridden with thesuppressDisabledCheckparameter. - Event handlers are automatically namespaced and cleaned up on destroy.
-
suppressDisabledCheck (default:
false)Type: BooleanWhether or not to bypass the disabled check. -
elementType: jQueryWhich element(s) to bind the event handlers to. If no element is provided,
this.elementis used. -
handlersType: ObjectA map in which the string keys represent the event type and optional selector for delegation, and the values represent a handler function to be called for the event.
_setOption( key, value )
_setOptions() method for each individual option. Widget state should be updated based on changes.
_show( element, option [, callback ] )
option values.
_super()
.call().
- This method does not accept any arguments.
_superApply( arguments )
.apply().
-
argumentsType: ArrayArray of arguments to pass to the parent method.
_trigger( type [, event ] [, data ] )
The option with the name equal to type is invoked as the callback.
The event name is the widget name + type.
Note: When providing data, you must provide all three parameters. If there is no event to pass along, just pass null.
destroy()
- This method does not accept any arguments.
disable()
- This method does not accept any arguments.
enable()
- This method does not accept any arguments.
option( optionName )Returns: Object
optionName.-
optionNameType: StringThe name of the option to get.
Invoke the method:
|
1
|
|
option()Returns: PlainObject
- This method does not accept any arguments.
Invoke the method:
|
1
|
|
option( optionName, value )
optionName.-
optionNameType: StringThe name of the option to set.
-
valueType: ObjectA value to set for the option.
Invoke the method:
|
1
|
|
option( options )
-
optionsType: ObjectA map of option-value pairs to set.
Invoke the method:
|
1
|
|
widget()Returns: jQuery
jQuery object containing the original element or other relevant generated element.
- This method does not accept any arguments.
Events
create( event, ui )Type: widgetcreate
Note: The ui object is empty but included for consistency with other events.
Initialize the widget with the create callback specified:
|
1
2
3
|
|
Bind an event listener to the widgetcreate event:
|
1
|
|