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.
Looking for tutorials about the widget factory? Check out the articles on the jQuery Learning Center.
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.
You can pass multiple options arguments. Those arguments will be merged into one object (similar to $.extend( true, target, object1, objectN )
). This is useful for sharing options between instances, while overriding some settings for each one:
1
2
3
|
|
All options passed on init are deep-copied to ensure the objects can be modified later without affecting the widget. Arrays are the only exception, they are referenced as-is. This exception is in place to support data-binding, where the data source has to be kept as a reference.
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 and lowercased. 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:
-
defaultElement: An element to use when a widget instance is constructed without providing an element. For example, since the progressbar's
defaultElement
is"<div>
",$.ui.progressbar({ value: 50 })
instantiates a progressbar widget instance on a newly created<div>
. -
document: The
document
that 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
namespace
of"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
widgetEventPrefix
of the draggable widget is"drag"
, therefore when a draggable is created, the name of the event fired is"dragcreate"
. By default thewidgetEventPrefix
of 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", {} )
,widgetFullName
will be"myNamespace-myWidget"
. -
widgetName: The name of the widget. For
$.widget( "myNamespace.myWidget", {} )
,widgetName
will be"myWidget"
. -
window: The
window
that the widget's element is within. Useful if you need to interact with widgets within iframes.