jQuery.widget.bridge.html 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>jQuery UI jQuery documentation</title>
  6. <style>
  7. body {
  8. font-family: "Trebuchet MS", "Arial", "Helvetica", "Verdana", "sans-serif"
  9. }
  10. .gutter {
  11. display: none;
  12. }
  13. </style>
  14. </head>
  15. <body>
  16. <script>{
  17. "title":
  18. "Widget Plugin Bridge",
  19. "excerpt":
  20. "Part of the jQuery Widget Factory is the jQuery.widget.bridge() method. This acts as the middleman between the object created by $.widget() and the jQuery API.",
  21. "termSlugs": {
  22. "category": [
  23. "utilities","widgets"
  24. ]
  25. }
  26. }</script><article id="jQuery-widget-bridge1" class="entry method"><h2 class="section-title"><span class="name">jQuery.widget.bridge( name, constructor )</span></h2>
  27. <div class="entry-wrapper">
  28. <p class="desc"><strong>Description: </strong>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.</p>
  29. <ul class="signatures"><li class="signature">
  30. <h4 class="name"><a id="jQuery-widget-bridge-name-constructor" href="#jQuery-widget-bridge-name-constructor"><span class="icon-link"></span>jQuery.widget.bridge( name, constructor )</a></h4>
  31. <ul>
  32. <li>
  33. <div><strong>name</strong></div>
  34. <div>Type: <a href="http://api.jquery.com/Types/#String">String</a>
  35. </div>
  36. <div>The name of the plugin to create.</div>
  37. </li>
  38. <li>
  39. <div><strong>constructor</strong></div>
  40. <div>Type: <a href="http://api.jquery.com/Types/#Function">Function</a>()</div>
  41. <div>The object to instantiate when the plugin is invoked.</div>
  42. </li>
  43. </ul>
  44. </li></ul>
  45. <div class="longdesc" id="entry-longdesc">
  46. <p><code>$.widget.bridge()</code> does a few things:</p>
  47. <ul>
  48. <li>Connects a regular JavaScript constructor to the jQuery API.</li>
  49. <li>Automatically creates instances of said object and stores it within the element's <code>$.data</code> cache.</li>
  50. <li>Allows calls to public methods.</li>
  51. <li>Prevents calls to private methods.</li>
  52. <li>Prevents method calls on uninitialized objects.</li>
  53. <li>Protects against multiple initializations.</li>
  54. </ul>
  55. <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>
  56. <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>
  57. <div class="syntaxhighlighter javascript nogutter">
  58. <table>
  59. <tbody>
  60. <tr>
  61. <td class="gutter">
  62. <div class="line n1">1</div>
  63. <div class="line n2">2</div>
  64. <div class="line n3">3</div>
  65. <div class="line n4">4</div>
  66. <div class="line n5">5</div>
  67. <div class="line n6">6</div>
  68. <div class="line n7">7</div>
  69. <div class="line n8">8</div>
  70. <div class="line n9">9</div>
  71. <div class="line n10">10</div>
  72. <div class="line n11">11</div>
  73. <div class="line n12">12</div>
  74. <div class="line n13">13</div>
  75. </td>
  76. <td class="code">
  77. <pre><div class="container"><div class="line"><code><span class="keyword">var</span> Highlighter = <span class="keyword">function</span>( options, element ) {</code></div></div><div class="container"><div class="line"><code> <span class="keyword">this</span>.options = options;</code></div></div><div class="container"><div class="line"><code> <span class="keyword">this</span>.element = $( element );</code></div></div><div class="container"><div class="line"><code> <span class="keyword">this</span>._set( <span class="number">800</span> );</code></div></div><div class="container"><div class="line"><code>};</code></div></div><div class="container"><div class="line"><code>Highlighter.prototype = {</code></div></div><div class="container"><div class="line"><code> toggle: <span class="keyword">function</span>() {</code></div></div><div class="container"><div class="line"><code> <span class="keyword">this</span>._set( <span class="keyword">this</span>.element.css( <span class="string">"font-weight"</span>) === <span class="number">400</span> ? <span class="number">800</span> : <span class="number">400</span> );</code></div></div><div class="container"><div class="line"><code> },</code></div></div><div class="container"><div class="line"><code> _set: <span class="keyword">function</span>(value) {</code></div></div><div class="container"><div class="line"><code> <span class="keyword">this</span>.element.css( <span class="string">"font-weight"</span>, value );</code></div></div><div class="container"><div class="line"><code> }</code></div></div><div class="container"><div class="line"><code>};</code></div></div></pre>
  78. </td>
  79. </tr>
  80. </tbody>
  81. </table>
  82. </div>
  83. <p>All you need here is a function that acts as the constructor, accepting two arguments:</p>
  84. <ul>
  85. <li>
  86. <code>options</code>: an object of configuration options</li>
  87. <li>
  88. <code>element</code>: the DOM element this instance was created on</li>
  89. </ul>
  90. <p>You can then hook this object up as a jQuery plugin using the bridge and use it on any jQuery object:</p>
  91. <div class="syntaxhighlighter javascript nogutter">
  92. <table>
  93. <tbody>
  94. <tr>
  95. <td class="gutter">
  96. <div class="line n1">1</div>
  97. <div class="line n2">2</div>
  98. <div class="line n3">3</div>
  99. <div class="line n4">4</div>
  100. <div class="line n5">5</div>
  101. <div class="line n6">6</div>
  102. <div class="line n7">7</div>
  103. <div class="line n8">8</div>
  104. </td>
  105. <td class="code">
  106. <pre><div class="container"><div class="line"><code><span class="comment">// Hook up the plugin</span></code></div></div><div class="container"><div class="line"><code>$.widget.bridge( <span class="string">"colorToggle"</span>, Highlighter );</code></div></div><div class="container"><div class="line"><code> </code></div></div><div class="container"><div class="line"><code><span class="comment">// Initialize it on divs</span></code></div></div><div class="container"><div class="line"><code>$( <span class="string">"div"</span> ).colorToggle().click(<span class="keyword">function</span>() {</code></div></div><div class="container"><div class="line"><code> <span class="comment">// Call the public method on click</span></code></div></div><div class="container"><div class="line"><code> $( <span class="keyword">this</span> ).colorToggle( <span class="string">"toggle"</span> );</code></div></div><div class="container"><div class="line"><code>});</code></div></div></pre>
  107. </td>
  108. </tr>
  109. </tbody>
  110. </table>
  111. </div>
  112. <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>
  113. <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>
  114. </div>
  115. </div></article>
  116. </body>
  117. </html>