Skip to content

Commit cde5d57

Browse files
committed
jQuery.widget: Document that instance can be called on any element to determine whether it is a widget. Fixes jquery#77.
jQuery.widget: More explicit wording to clarify the difference between using :data and instance(). jQuery.widget: Minor updates
1 parent 06d80fe commit cde5d57

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

entries/jQuery.widget.xml

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,17 +87,30 @@
8787

8888
<h3>Instance</h3>
8989

90-
<p>The widget's instance can be retrieved from a given element using the <a href="#method-instance">instance method</a>.</p>
90+
<p>The widget's instance can be retrieved from a given element using the <a href="#method-instance"><code>instance()</code></a> method.</p>
9191

9292
<pre><code>
9393
$( "#elem" ).progressbar( "instance" );
9494
</code></pre>
9595

96-
<p>The instance is stored using <a href="http://api.jquery.com/jQuery.data/"><code>jQuery.data()</code></a> with the widget's full name as the key. Therefore, you can use the <a href="/data-selector"><code>:data</code></a> selector to determine whether an element has a given widget bound to it.</p>
96+
<p>If the <code>instance()</code> method is called on an element that is not associated with the widget, <code>undefined</code> is returned.</p>
97+
98+
<pre><code>
99+
$( "#not-a-progressbar" ).progressbar( "instance" ); // undefined
100+
</code></pre>
101+
102+
<p>The instance is stored using <a href="http://api.jquery.com/jQuery.data/"><code>jQuery.data()</code></a> with the widget's full name as the key. Therefore, the <a href="/data-selector/"><code>:data</code></a> selector can also determine whether an element has a given widget bound to it.</p>
97103

98104
<pre><code>
99105
$( "#elem" ).is( ":data( 'ui-progressbar' )" ); // true
100-
$( "#elem" ).is( ":data( 'ui-draggable' )" ); //false
106+
$( "#elem" ).is( ":data( 'ui-draggable' )" ); // false
107+
</code></pre>
108+
109+
<p>Unlike <code>instance()</code>, <code>:data</code> can be used even if the widget being tested for has not loaded.</p>
110+
111+
<pre><code>
112+
$( "#elem" ).nonExistentWidget( "instance" ); // TypeError
113+
$( "#elem" ).is( ":data( 'ui-nonExistentWidget' )" ); // false
101114
</code></pre>
102115

103116
<p>You can also use <code>:data</code> to get a list of all elements that are instances of a given widget.</p>

includes/widget-method-instance.xml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?xml version="1.0"?>
2-
<method name="instance">
2+
<method name="instance" return="Object">
33
<desc>
4-
Retrieves the <placeholder name="name"/>'s instance object.
4+
<p>Retrieves the <placeholder name="name"/>'s instance object. If the element does not have an associated instance, <code>undefined</code> is returned.</p>
5+
6+
<p>Unlike other widget methods, <code>instance()</code> is safe to call on any element after the <placeholder name="name"/> plugin has loaded.</p>
57
</desc>
68
</method>

0 commit comments

Comments
 (0)