2
0

ZendX_JQuery-View-Helpers.xml 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect2 id="zendx.jquery.view.helpers">
  4. <title>JQuery Helpers</title>
  5. <sect3 id="zendx.jquery.view.helpers.ajaxlink">
  6. <title>AjaxLink Helper</title>
  7. <para>The AjaxLink helper uses jQuery's ajax capabilities to offer the creation of links that do ajax requests and
  8. inject the response into a chosen DOM element. It also offers the possibility to append simple jQuery effects
  9. to both the link and the response DOM element. A simple example introduces its functionality:</para>
  10. <programlisting language="php"><![CDATA[
  11. <!-- Inside your View Object -->
  12. <div id="container"></div>
  13. <?php echo $this->view->ajaxLink("Link Name",
  14. "url.php",
  15. array('update' => '#container')); ?>
  16. ]]></programlisting>
  17. <para>This example creates a link with the label "Link Name" that fires an ajax request to url.php
  18. upon click and renders the response into the div container "#container". The function header
  19. for the ajaxLink is as follows: <code>function ajaxLink($label, $url, $options, $params);</code>
  20. The options array is very powerful and offers you lots of functionality to customize your
  21. ajax requests.</para>
  22. <para>Available options are:</para>
  23. <table id="zendx.jquery.view.helpers.ajaxlink.table">
  24. <title>AjaxLink options</title>
  25. <tgroup cols="4">
  26. <thead>
  27. <row>
  28. <entry>Option</entry>
  29. <entry>Data Type</entry>
  30. <entry>Default Value</entry>
  31. <entry>Description</entry>
  32. </row>
  33. </thead>
  34. <tbody>
  35. <row>
  36. <entry><code>update</code></entry>
  37. <entry><code>string</code></entry>
  38. <entry><code>false</code></entry>
  39. <entry>
  40. Container to inject response content into, use jQuery CSS Selector syntax, ie. "#container" or ".box"
  41. </entry>
  42. </row>
  43. <row>
  44. <entry><code>method</code></entry>
  45. <entry><code>string</code></entry>
  46. <entry><code>Implicit GET or POST</code></entry>
  47. <entry>Request method, is implicitly chosen as GET when no parameters given and POST when parameters given.</entry>
  48. </row>
  49. <row>
  50. <entry><code>complete</code></entry>
  51. <entry><code>string</code></entry>
  52. <entry><code>false</code></entry>
  53. <entry>Javascript callback executed, when ajax request is complete. This option allows for shortcut effects, see next section.</entry>
  54. </row>
  55. <row>
  56. <entry><code>beforeSend</code></entry>
  57. <entry><code>string</code></entry>
  58. <entry><code>false</code></entry>
  59. <entry>Javascript callback executed right before ajax request is started. This option allows for shortcut effects, see next section.</entry>
  60. </row>
  61. <row>
  62. <entry><code>noscript</code></entry>
  63. <entry><code>boolean</code></entry>
  64. <entry><code>true</code></entry>
  65. <entry>If true the link generated will contain a href attribute to the given link for non-javascript enabled browsers. If false href will resolve to "#".</entry>
  66. </row>
  67. <row>
  68. <entry><code>dataType</code></entry>
  69. <entry><code>string</code></entry>
  70. <entry><code>html</code></entry>
  71. <entry>What type of data is the Ajax Response of? Possible are Html, Text, Json. Processing Json responses has to be done with custom "complete" callback functions.</entry>
  72. </row>
  73. <row>
  74. <entry><code>attribs</code></entry>
  75. <entry><code>array</code></entry>
  76. <entry><code>null</code></entry>
  77. <entry>Additional HTML attributes the ajaxable link should have.</entry>
  78. </row>
  79. <row>
  80. <entry><code>title, id, class</code></entry>
  81. <entry><code>string</code></entry>
  82. <entry><code>false</code></entry>
  83. <entry>Convenience shortcuts for HTML Attributes.</entry>
  84. </row>
  85. <row>
  86. <entry><code>inline</code></entry>
  87. <entry><code>boolean</code></entry>
  88. <entry><code>false</code></entry>
  89. <entry>Although far from best practice, you can set javascript for this link inline in "onclick" attribute.</entry>
  90. </row>
  91. </tbody>
  92. </tgroup>
  93. </table>
  94. <para>To enlighten the usage of this helper it is best to show another bunch of more complex examples. This
  95. example assumes that you have only one view object that you want to display and don't care a lot about
  96. html best practices, since we have to output the jQuery environment just before the closing body tag.</para>
  97. <programlisting language="php"><![CDATA[
  98. <html>
  99. <head>
  100. <title>Zend Framework jQuery AjaxLink Example</title>
  101. <script language="javascript"
  102. type="text/javascript"
  103. src="myCallbackFuncs.js"></script>
  104. </head>
  105. <body>
  106. <!-- without echoing jQuery this following -->
  107. <!-- list only prints a list of for links -->
  108. <ul>
  109. <li>
  110. <?php echo $this->ajaxLink("Example 1",
  111. "/ctrl/action1",
  112. array('update' => '#content',
  113. 'noscript' => false,
  114. 'method' => 'POST')); ?>
  115. </li>
  116. <li>
  117. <?php echo $this->ajaxLink("Example 2",
  118. "/ctrl/action2",
  119. array('update' => '#content',
  120. 'class' => 'someLink'),
  121. array('param1' => 'value1',
  122. 'param2' => 'value2')); ?>
  123. </li>
  124. <li><?php echo $this->ajaxLink("Example 3",
  125. "/ctrl/action3",
  126. array('dataType' => 'json',
  127. 'complete' =>
  128. 'alert(data)')); ?>
  129. </li>
  130. <li><?php echo $this->ajaxLink("Example 4",
  131. "/ctrl/action4",
  132. array('beforeSend' => 'hide',
  133. 'complete' => 'show')); ?>
  134. </li>
  135. <li>
  136. <?php echo $this->ajaxLink("Example 5",
  137. "/ctrl/action5",
  138. array(
  139. 'beforeSend' =>
  140. 'myBeforeSendCallbackJsFunc();',
  141. 'complete' =>
  142. 'myCompleteCallbackJsFunc(data);')
  143. ); ?>
  144. </li>
  145. </ul>
  146. <!-- only at this point the javascript is printed to sreen -->
  147. <?php echo $this->jQuery(); ?>
  148. </body>
  149. </html>
  150. ]]></programlisting>
  151. <para>You might have already seen that the 'update', 'complete', and 'beforeSend' options have to be executed in specific order and syntax so that you cannot
  152. use those callbacks and override their behaviour completely when you are using <methodname>ajaxLink()</methodname>. For larger use cases you will probably want to
  153. write the request via jQuery on your own. The primary use case for the callbacks is effect usage, other uses may very well become hard to maintain.
  154. As shown in Example Link 5, you can also forward the beforeSend/complete Callbacks to your own javascript functions.</para>
  155. <sect4 id="zendx.jquery.view.helpers.ajaxlink.effects">
  156. <title>Shortcut Effects</title>
  157. <para>You can use shortcut effect names to make your links actions more fancy. For example the Container that will contain
  158. the ajax response may very well be invisible in the first place. Additionally you can use shortcut effects on the link
  159. to hide it after clicking. The following effects can be used for callbacks:</para>
  160. <itemizedlist>
  161. <listitem><para><code>complete</code> callback: 'show', 'showslow', 'shownormal', 'showfast', 'fadein', 'fadeinslow',
  162. 'fadeinfast', 'slidedown', 'slidedownslow', 'slidedownfast'. These all correspond to the jQuery effects
  163. fadeIn(), show() and slideDown() and will be executed on the container specified in <code>update</code>.</para></listitem>
  164. <listitem><para><code>beforeSend</code> callback: 'fadeout', 'fadeoutslow', 'fadeoutfast', 'hide',
  165. 'hideslow', 'hidefast', 'slideup'. These correspond to the jQuery effects fadeOut(), hide(), slideUp() and
  166. are executed on the clicked link.</para></listitem>
  167. </itemizedlist>
  168. <programlisting language="php"><![CDATA[
  169. <?php echo $this->ajaxLink("Example 6",
  170. "/ctrl/action6",
  171. array('beforeSend' => 'hide',
  172. 'complete' => 'show')); ?>
  173. ]]></programlisting>
  174. </sect4>
  175. </sect3>
  176. <sect3 id="zendx.jquery.view.helpers.ui">
  177. <title>jQuery UI Library Helpers</title>
  178. <para>The jQuery UI Library offers a range of layout and form specific widgets that are integrated into the
  179. Zend Framework via View Helpers. The form-elements are easy to handle and will be described first, whereas
  180. the layout specific widgets are a bit more complex to use.</para>
  181. <sect4 id="zendx.jquery.view.helpers.ui.form">
  182. <title>jQuery UI Form Helpers</title>
  183. <para>The method signature for all form view helpers closely resembles the Dojo View helpers signature,
  184. <methodname>helper($id, $value, $params, $attribs)</methodname>. A description of the parameters follows:</para>
  185. <itemizedlist>
  186. <listitem><para><varname>$id</varname>: Will act as the identifier name for the helper element inside a form. If in the attributes
  187. no id element is given, this will also become the form element id, that has to be unique across
  188. the DOM.</para></listitem>
  189. <listitem><para><varname>$value</varname>: Default value of the element.</para></listitem>
  190. <listitem><para><varname>$params</varname>: Widget specific parameters that customize the look and feel
  191. of the widget. These options are unique to each widget and <ulink url="http://docs.jquery.com/UI">
  192. described in the jQuery UI documentation</ulink>. The data is casted to JSON, so make sure
  193. to use the <classname>Zend_Json_Expr</classname> class to mark executable javascript as safe.</para></listitem>
  194. <listitem><para><varname>$attribs</varname>: HTML Attributes of the Form Helper</para></listitem>
  195. </itemizedlist>
  196. <para>The following UI widgets are available as form view helpers. Make sure you use the correct
  197. version of jQuery UI library to be able to use them. The Google CDN always offers you the latest
  198. released version.</para>
  199. <itemizedlist>
  200. <listitem><para><methodname>autoComplete($id, $value, $params, $attribs)</methodname>: The AutoComplete View helper
  201. is part of jQuery UI since version 1.8 and creates a text field and registeres
  202. it to have auto complete functionality. The completion data source has to be given as jQuery
  203. related parameters 'url' or 'data' as described in the jQuery UI manual.
  204. </para></listitem>
  205. <listitem><para><methodname>colorPicker($id, $value, $params, $attribs)</methodname>: ColorPicker is still
  206. a ZendX_JQuery element for legacy reason, but was removed from jQuery UI completly.
  207. </para></listitem>
  208. <listitem><para><methodname>datePicker($id, $value, $params, $attribs)</methodname>: Create a DatePicker
  209. inside a text field. This widget is available since jQuery UI 1.5 and can therefore currently be used
  210. with the Google CDN. Using the 'handles' option to create multiple handles overwrites the default set value
  211. and the jQuery parameter 'startValue' internally inside the view helper.</para></listitem>
  212. <listitem><para><methodname>slider($id, $value, $params, $attribs)</methodname>: Create a Sliding element
  213. that updates its value into a hidden form field. Available since jQuery UI 1.5.</para></listitem>
  214. <listitem><para><methodname>spinner($id, $value, $params, $attribs)</methodname>: Create a Spinner element
  215. that can spin through numeric values in a specified range. This element was removed from
  216. the 1.6 jQuery UI release and has not been re-released yet.</para></listitem>
  217. </itemizedlist>
  218. <example id="zendx.jquery.view.helpers.form.example">
  219. <title>Showing jQuery Form View Helper Usage</title>
  220. <para>In this example we want to simulate a fictional web application that offers auctions
  221. on travel locations. A user may specify a city to travel, a start and end date, and a
  222. maximum amount of money he is willing to pay. Therefore we need an autoComplete field
  223. for all the currently known travel locations, a date picker for start and end dates
  224. and a spinner to specify the amount.</para>
  225. <programlisting language="php"><![CDATA[
  226. <form method="post" action="bid.php">
  227. <label for="locaction">Where do you want to travel?</label>
  228. <?php echo $this->autoComplete("location",
  229. "",
  230. array('source' => array('New York',
  231. 'Mexico City',
  232. 'Sydney',
  233. 'Ruegen',
  234. 'Baden Baden'))); ?>
  235. <br />
  236. <label for="startDate">Travel Start Date:</label>
  237. <?php echo $this->datePicker("startDate", '',
  238. array(
  239. 'defaultDate' => '+7',
  240. 'minDate' => '+7',
  241. 'onClose' => new Zend_Json_Expr('myJsonFuncCechkingValidity'))); ?>
  242. <br />
  243. <label for="startDate">Travel End Date:</label>
  244. <?php echo $this->datePicker("endDate", '',
  245. array(
  246. 'defaultDate' => '+14',
  247. 'minDate' => '+7',
  248. 'onClose' => new Zend_Json_Expr('myJsonFuncCechkingValidity'))); ?>
  249. <br />
  250. <label for="bid">Your Bid:</label>
  251. <?php echo $this->spinner("bid",
  252. "",
  253. array('min' => 1205.50,
  254. 'max' => 10000,
  255. 'start' => 1205.50,
  256. 'currency' => '€')); ?>
  257. <br />
  258. <input type="submit" value="Bid!" />
  259. </form>
  260. ]]></programlisting>
  261. <para>You can see the use of jQuery UI Widget specific parameters. These all correspond to those given in the jQuery UI docs
  262. and are converted to JSON and handed through to the view script.</para>
  263. </example>
  264. </sect4>
  265. <sect4 id="zendx.jquery.view.helpers.ui.actionhelper">
  266. <title>Using an Action Helper to Send Data to AutoComplete</title>
  267. <para>The jQuery UI Autocomplete Widget can load data from a remote location
  268. rather than from an javascript array, making its usage really useful. Zend
  269. Framework currently providers a bunch of server-side AutoComplete
  270. Helpers and there is one for jQuery too. You register the helper
  271. to the controller helper broker and it takes care of disabling layouts
  272. and renders an array of data correctly to be read by the AutoComplete field.
  273. To use the Action Helper you have to put this rather long statement into
  274. your bootstrap or Controller initialization function:
  275. </para>
  276. <programlisting language="php"><![CDATA[
  277. Zend_Controller_Action_HelperBroker::addHelper(
  278. new ZendX_JQuery_Controller_Action_Helper_AutoComplete()
  279. );
  280. ]]></programlisting>
  281. <para>You can then directly call the helper to render AutoComplete Output in
  282. your Controller</para>
  283. <programlisting language="php"><![CDATA[
  284. class MyIndexController extends Zend_Controller_Action
  285. {
  286. public function autocompleteAction()
  287. {
  288. // The data sent via the ajax call is inside $_GET['q']
  289. $filter = $_GET['q'];
  290. // Disable Layout and stuff, just displaying AutoComplete Information.
  291. $this->_helper->autoComplete(array("New York", "Bonn", "Tokio"));
  292. }
  293. }
  294. ]]></programlisting>
  295. </sect4>
  296. <sect4 id="zendx.jquery.view.helpers.ui.layout">
  297. <title>jQuery UI Layout Helpers</title>
  298. <para>There is a wide range of Layout helpers that the UI library offers. The ones covered by Zend Framework view helpers are
  299. Accordion, Dialog, Tabs. Dialog is the most simple one, whereas Accordion and Tab extend a common abstract class and
  300. offer a secondary view helper for pane generation. The following view helpers exist in the jQuery view helpers collection,
  301. an example accompanies them to show their usage.</para>
  302. <itemizedlist>
  303. <listitem><para><methodname>dialogContainer($id, $content, $params, $attribs)</methodname>: Create a Dialog Box that is rendered with
  304. the given content.on startup. If the option 'autoOpen' set to false is specified the box will not be displayed
  305. on load but can be shown with the additional <methodname>dialog("open")</methodname> javascript function. See UI docs
  306. for details.</para></listitem>
  307. <listitem><para><methodname>tabPane($id, $content, $options)</methodname>: Add a new pane to a tab container with the given <varname>$id</varname>.
  308. The given <varname>$content</varname> is shown in this tab pane. To set the title use <varname>$options['title']</varname>.
  309. If <varname>$options['contentUrl']</varname> is set, the content of the tab is requested via ajax on tab activation.
  310. </para></listitem>
  311. <listitem><para><methodname>tabContainer($id, $params, $attribs)</methodname>: Render a tab container with all the currently registered
  312. panes. This view helper also offers to add panes with the following syntax:
  313. <code>$this->tabContainer()->addPane($id, $label, $content, $options)</code>.
  314. </para></listitem>
  315. <listitem><para><methodname>accordionPane($id, $content, $options)</methodname>: Add a new pane to the accordion container with the given <varname>$id</varname>.
  316. The given <varname>$content</varname> is shown in this tab pane. To set the title use <varname>$options['title']</varname>.
  317. </para></listitem>
  318. <listitem><para><methodname>accordionContainer($id, $params, $attribs)</methodname>: Render an accordion container with all the currently registered
  319. panes. This view helper also offers to add panes with the following syntax:
  320. <code>$this->accordionContainer()->addPane($id, $label, $content, $options)</code>.
  321. </para></listitem>
  322. </itemizedlist>
  323. <example id="zendx.jquery.view.helpers.ui.layout.example">
  324. <title>Showing the latest news in a Tab Container</title>
  325. <para>For this example we assume the developer already wrote the controller and model side of the script and assigned
  326. an array of news items to the view script. This array contains at most 5 news elements, so we don't have to care
  327. about the tab container getting to many tabs.</para>
  328. <programlisting language="php"><![CDATA[
  329. <?php foreach($this->news AS $article): ?>
  330. <?php $this->tabPane("newstab",
  331. $article->body,
  332. array('title' => $article->title)); ?>
  333. <?php endforeach; ?>
  334. <h2>Latest News</h2>
  335. <?php echo $this->tabContainer("newstab",
  336. array(),
  337. array('class' => 'flora')); ?>
  338. ]]></programlisting>
  339. </example>
  340. </sect4>
  341. </sect3>
  342. </sect2>