|
| 1 | +<?xml version="1.0"?> |
| 2 | +<entries> |
| 3 | + <entry type="method" name="jQuery.widget.bridge"> |
| 4 | + <title>Widget Plugin Bridge</title> |
| 5 | + <signature> |
| 6 | + <argument name="name" type="String"> |
| 7 | + <desc>The name of the plugin to create.</desc> |
| 8 | + </argument> |
| 9 | + <argument name="constructor" type="Function"> |
| 10 | + <desc>The object to instantiate when the plugin is invoked.</desc> |
| 11 | + </argument> |
| 12 | + </signature> |
| 13 | + <desc>Part of the <a href="/jQuery.widget/">jQuery Widget Factory</a> is the <code>jQuery.widget.bridge()</code> method. This acts as the middleman between the object created by <code>$.widget()</code> and the jQuery API.</desc> |
| 14 | + <longdesc> |
| 15 | + <p><code>$.widget.bridge()</code> does a few things:</p> |
| 16 | + <ul> |
| 17 | + <li>Connects a regular JavaScript constructor to the jQuery API.</li> |
| 18 | + <li>Automatically creates instances of said object and stores it within the element's <code>$.data</code> cache.</li> |
| 19 | + <li>Allows calls to public methods.</li> |
| 20 | + <li>Prevents calls to private methods.</li> |
| 21 | + <li>Prevents method calls on uninitialized objects.</li> |
| 22 | + <li>Protects against multiple initializations.</li> |
| 23 | + </ul> |
| 24 | + <p>jQuery UI widgets are created using <code>$.widget( "foo.bar", {} );</code> syntax to define an object from which instances will be created. Given a DOM structure with five <code>.foo</code>'s, <code>$( ".foo" ).bar();</code> will create five instances of your "bar" object. <code>$.widget.bridge()</code> works inside the factory by taking your base "bar" object and giving it a public API. Therefore, you can create instances by writing <code>$( ".foo" ).bar()</code>, and call methods by writing <code>$( ".foo" ).bar( "baz" )</code>.</p> |
| 25 | + <p>If all you want is one-time initialization and calling methods, your object passed to <code>jQuery.widget.bridge()</code> can be very minimal:</p> |
| 26 | + <pre><code> |
| 27 | + var Highlighter = function( options, element ) { |
| 28 | + this.options = options; |
| 29 | + this.element = $( element ); |
| 30 | + this._set( 800 ); |
| 31 | + }; |
| 32 | + Highlighter.prototype = { |
| 33 | + toggle: function() { |
| 34 | + this._set( this.element.css( "font-weight") === 400 ? 800 : 400 ); |
| 35 | + }, |
| 36 | + _set: function(value) { |
| 37 | + this.element.css( "font-weight", value ); |
| 38 | + } |
| 39 | + }; |
| 40 | + </code></pre> |
| 41 | + <p>All you need here is a function that acts as the constructor, accepting two arguments:</p> |
| 42 | + <ul> |
| 43 | + <li><code>options</code>: an object of configuration options</li> |
| 44 | + <li><code>element</code>: the DOM element this instance was created on</li> |
| 45 | + </ul> |
| 46 | + <p>You can then hook this object up as a jQuery plugin using the bridge and use it on any jQuery object:</p> |
| 47 | + <pre><code> |
| 48 | + // Hook up the plugin |
| 49 | + $.widget.bridge( "colorToggle", Highlighter ); |
| 50 | + |
| 51 | + // Initialize it on divs |
| 52 | + $( "div" ).colorToggle().click(function() { |
| 53 | + // Call the public method on click |
| 54 | + $( this ).colorToggle( "toggle" ); |
| 55 | + }); |
| 56 | + </code></pre> |
| 57 | + <p>To use all the features of the bridge, your object also needs to have an <code>_init()</code> method on the prototype. This will get called whenever the plugin is invoked while an instance already exists. In that case you also need to have an <code>option()</code> method. This will be invoked with the options as the first argument. If there were none, the argument will be an empty object. For a proper implementation of the <code>option</code> method, check out the implementation of <a href="/jQuery.widget/#jQuery-Widget2"><code>$.Widget</code></a>.</p> |
| 58 | + <p>There is one optional property the bridge will use, if present: If your object's prototype has a <code>widgetFullName</code> property, this will be used as the key for storing and retrieving the instance. Otherwise, the name argument will be used.</p> |
| 59 | + </longdesc> |
| 60 | + <category slug="utilities"/> |
| 61 | + <category slug="widgets"/> |
| 62 | + </entry> |
| 63 | +</entries> |
0 commit comments