Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: cb2c84e882
Fetching contributors…

Cannot retrieve contributors at this time

284 lines (259 sloc) 14.187 kb
<?xml version="1.0"?>
<entries>
<entry type="method" name="jQuery.widget">
<title>Widget Factory</title>
<signature>
<argument name="name" type="String">
<desc>The name of the widget to create, including the namespace.</desc>
</argument>
<argument name="base" type="Function" optional="true">
<desc>The base widget to inherit from. This must be a constructor that can be instantiated with the `new` keyword. Defaults to <code>jQuery.Widget</code>.</desc>
</argument>
<argument name="prototype" type="PlainObject">
<desc>The object to use as a prototype for the widget.</desc>
</argument>
</signature>
<desc>Create stateful jQuery plugins using the same abstraction as all jQuery UI widgets.</desc>
<longdesc>
<p>You can create new widgets from scratch, using just the <code>$.Widget</code> 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.</p>
<p>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.</p>
<p><em>Note: This documentation shows examples using the <a href="/progressbar">progressbar widget</a> but the syntax is the same for every widget.</em></p>
<h3>Initialization</h3>
<p>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 initalized. To initialize a widget, we simply call the plugin on one or more elements.</p>
<pre><code>
$( "#elem" ).progressbar();
</code></pre>
<p>This will initialize each element in the jQuery object, in this case the element with an id of <code>"elem"</code>. Because we called the <code>progressbar()</code> method with no parameters, the widget is initialized with its default options. We can pass a set of options during initialization in order to override the default options.</p>
<pre><code>
$( "#elem" ).progressbar({ value: 20 });
</code></pre>
<p>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.</p>
<p>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.</p>
<h3>Methods</h3>
<p>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 <code>value()</code> method on our progressbar widget, we would use:</p>
<pre><code>
$( "#elem" ).progressbar( "value" );
</code></pre>
<p>If the method accepts parameters, we can pass them after the method name. For example, to pass the parameter <code>40</code> to the <code>value()</code> method, we can use:</p>
<pre><code>
$( "#elem" ).progressbar( "value", 40 );
</code></pre>
<p>Just like other methods in jQuery, most widget methods return the jQuery object for chaining.</p>
<pre><code>
$( "#elem" )
.progressbar( "value", 90 )
.addClass( "almost-done" );
</code></pre>
<p>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.</p>
<h3>Events</h3>
<p>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 <code>change</code> event which is triggered whenever the value changes.</p>
<pre><code>
$( "#elem" ).bind( "progressbarchange", function() {
alert( "The value has changed!" );
});
</code></pre>
<p>Each event has a corresponding callback, which is exposed as an option. We can hook into progressbar's <code>change</code> callback instead of binding to the <code>progressbarchange</code> event, if we want to.</p>
<pre><code>
$( "#elem" ).progressbar({
change: function() {
alert( "The value has changed!" );
}
});
</code></pre>
<p>All widgets have a <code>create</code> event which is triggered upon instantiation.</p>
</longdesc>
<category slug="utilities"/>
</entry>
<entry type="widget" name="jQuery.Widget" animated-element="element"
widget-element="original element or other relevant generated element">
<title>Base Widget</title>
<desc>The base widget used by the widget factory.</desc>
<options>
<xi:include href="../includes/widget-option-disabled.xml" xmlns:xi="http://www.w3.org/2003/XInclude"/>
<xi:include href="../includes/widget-option-hide.xml" xmlns:xi="http://www.w3.org/2003/XInclude"/>
<xi:include href="../includes/widget-option-show.xml" xmlns:xi="http://www.w3.org/2003/XInclude"/>
</options>
<methods>
<xi:include href="../includes/widget-method-destroy.xml" xmlns:xi="http://www.w3.org/2003/XInclude"/>
<xi:include href="../includes/widget-method-disable.xml" xmlns:xi="http://www.w3.org/2003/XInclude"/>
<xi:include href="../includes/widget-method-enable.xml" xmlns:xi="http://www.w3.org/2003/XInclude"/>
<xi:include href="../includes/widget-method-option.xml" xmlns:xi="http://www.w3.org/2003/XInclude"/>
<xi:include href="../includes/widget-method-widget.xml" xmlns:xi="http://www.w3.org/2003/XInclude"/>
<method name="_create">
<desc>
The <code>_create()</code> method is the widget's constructor.
There are no parameters, but <code>this.element</code> and <code>this.options</code> are already set.
</desc>
</method>
<method name="_destroy">
<desc>
The public <a href="#method-destroy"><code>destroy()</code></a> method cleans up all common data, events, etc. and then delegates out to <code>_destroy()</code> for custom, widget-specific, cleanup.
</desc>
</method>
<method name="_getCreateEventData" return="Object">
<desc>
All widgets trigger the <a href="#event-create"><code>create</code></a> event. By default, no data is provided in the event, but this method can return an object which will be passed as the <code>create</code> event's data.
</desc>
</method>
<method name="_getCreateOptions" return="Object">
<desc>
This method allows the widget to define a custom method for defining options during instantiation. This user-provided options override the options returned by this method which override the default options.
</desc>
</method>
<method name="_init">
<desc>
Widgets have the concept of initialization that is distinct from creation. Any time the plugin is called with no arguments or with only an option hash, the widget is initialized; this includes when the widget is created.
<p><em>Note: Initialization should only be handled if there is a logical action to perform on successive calls to the widget with no arguments.</em></p>
</desc>
</method>
<method name="_setOptions">
<desc>
Called whenever the <a href="#method-option"><code>option()</code></a> method is called, regardless of the form in which the <code>option()</code> method was called.
<p>Overriding this is useful if you can defer processor-intensive changes for multiple option changes.</p>
</desc>
<argument name="options" type="Object">
<desc>A map of option-value pairs to set.</desc>
</argument>
</method>
<method name="_setOption">
<desc>
Called from the <a href="#method-_setOptions"><code>_setOptions()</code></a> method for each individual option. Widget state should be updated based on changes.
</desc>
<argument name="key" type="String">
<desc>The name of the option to set.</desc>
</argument>
<argument name="value" type="Object">
<desc>A value to set for the option.</desc>
</argument>
</method>
<method name="_on">
<desc>
Binds event handlers to the specified element(s). Delegation is supported via selectors inside the event names, e.g., "<code>click .foo</code>". The <code>_on()</code> method provides several benefits of direct event binding:
<ul>
<li>Maintains proper <code>this</code> context inside the handlers.</li>
<li>Automatically handles disabled widgets: If the widget is disabled or the event occurs on an element with the <code>ui-state-disabled</code> class, the event handler is not invoked.</li>
<li>Event handlers are automatically namespaced and cleaned up on destroy.</li>
</ul>
</desc>
<argument name="element" type="jQuery" optional="true">
<desc>Which element(s) to bind the event handlers to. If no element is provided, <code>this.element</code> is used.</desc>
</argument>
<argument name="handlers" type="Object">
<desc>
A 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.
</desc>
</argument>
</method>
<method name="_off">
<desc>
Unbinds event handlers from the specified element(s).
</desc>
<argument name="element" type="jQuery">
<desc>
The element(s) to unbind the event handlers from. Unlike the <code>_on()</code> method, the elements are required for <code>_off()</code>.
</desc>
</argument>
<argument name="eventName" type="String">
<desc>One or more space-separated event types.</desc>
</argument>
</method>
<method name="_super">
<desc>
Invokes the method of the same name from the parent widget, with any specified arguments. Essentially <code>.call()</code>.
</desc>
</method>
<method name="_superApply">
<desc>
Invokes the method of the same name from the parent widget, with the array of arguments. Essentially <code>.apply()</code>.
</desc>
<argument name="arguments" type="Array">
<desc>Array of arguments to pass to the parent method.</desc>
</argument>
</method>
<method name="_delay" return="Number">
<desc>
Invokes the provided function after a specified delay. Keeps <code>this</code> context correct. Essentially <code>setTimeout()</code>.
<p>Returns the timeout ID for use with <code>clearTimeout()</code>.</p>
</desc>
<argument name="fn">
<desc>The function to invoke. Can also be the name of a method on the widget.</desc>
<type name="Function"/>
<type name="String"/>
</argument>
<argument name="delay" type="Number" optional="true">
<desc>The number of milliseconds to wait before invoking the function. Deafults to <code>0</code>.</desc>
</argument>
</method>
<method name="_hoverable">
<desc>
Sets up <code>element</code> to apply the <code>ui-state-hover</code> class on hover.
<p>The event handlers are automatically cleaned up on destroy.</p>
</desc>
<argument name="element" type="jQuery">
<desc>The element(s) to apply the hoverable behavior to.</desc>
</argument>
</method>
<method name="_focusable">
<desc>
Sets up <code>element</code> to apply the <code>ui-state-focus</code> class on focus.
<p>The event handlers are automatically cleaned up on destroy.</p>
</desc>
<argument name="element" type="jQuery">
<desc>The element(s) to apply the focusable behavior to.</desc>
</argument>
</method>
<method name="_trigger">
<desc>
Triggers an event and its associated callback.
<p>The option with the name equal to type is invoked as the callback.</p>
<p>The event name is the widget name + type.</p>
<p><em>Note: When providing data, you must provide all three parameters. If there is no event to pass along, just pass <code>null</code>.</em></p>
</desc>
<argument name="type" type="String">
<desc>The <code>type</code> should match the name of a callback option. The full event type will be generated automatically.</desc>
</argument>
<argument name="event" type="Event" optional="true">
<desc>The original event that caused this event to occur; useful for providing context to the listener.</desc>
</argument>
<argument name="data" type="Object" optional="true">
<desc>A hash of data associated with the event.</desc>
</argument>
</method>
<method name="_show">
<desc>
Shows an element immediately, using built-in animation methods, or using custom effects.
See the <a href="#option-show">show</a> option for possible <code>option</code> values.
</desc>
<argument name="element" type="jQuery">
<desc>The element(s) to show.</desc>
</argument>
<argument name="option" type="Object">
<desc>The settings defining how to show the element.</desc>
</argument>
<argument name="callback" type="Function" optional="true">
<desc>Callback to invoke after the element has been fully shown.</desc>
</argument>
</method>
<method name="_hide">
<desc>
Hides an element immediately, using built-in animation methods, or using custom effects.
See the <a href="#option-hide">hide</a> option for possible <code>option</code> values.
</desc>
<argument name="element" type="jQuery">
<desc>The element(s) to hide.</desc>
</argument>
<argument name="option" type="Object">
<desc>The settings defining how to hide the element.</desc>
</argument>
<argument name="callback" type="Function" optional="true">
<desc>Callback to invoke after the element has been fully hidden.</desc>
</argument>
</method>
</methods>
<events>
<xi:include href="../includes/widget-event-create.xml" xmlns:xi="http://www.w3.org/2003/XInclude"/>
</events>
<category slug="utilities"/>
<category slug="widgets"/>
</entry>
</entries>
Jump to Line
Something went wrong with that request. Please try again.