ZendX_JQuery-View-JQuery.xml 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect2 id="zendx.jquery.view.jquery">
  4. <title>jQuery() View Helper</title>
  5. <para>
  6. The <methodname>jQuery()</methodname> view helper simplifies setup of your jQuery environment
  7. in your application. It takes care of loading the core and ui library dependencies if necessary
  8. and acts as a stack for all the registered onLoad javascript statements. All jQuery view helpers
  9. put their javascript code onto this stack. It acts as a collector for everything jQuery in your
  10. application with the following responsibilities:
  11. </para>
  12. <itemizedlist>
  13. <listitem><para>Handling deployment of CDN or a local path jQuery Core
  14. and UI libraries.</para></listitem>
  15. <listitem><para>Handling <ulink url="http://api.jquery.com/ready">$(document).ready()</ulink> events.</para></listitem>
  16. <listitem><para>Specifying additional stylesheet themes to use.</para></listitem>
  17. </itemizedlist>
  18. <para>
  19. The <methodname>jQuery()</methodname> view helper implementation, like its <methodname>dojo()</methodname> pendant,
  20. follows the placeholder architecture implementation; the data set in it persists between view
  21. objects, and may be directly echo'd from your layout script. Since views specified in a Zend_Layout
  22. script file are rendered before the layout itself, the <methodname>jQuery()</methodname> helper can act as a
  23. stack for jQuery statements and render them into the head segment of the html page.
  24. </para>
  25. <para>Contrary to Dojo, themes cannot be loaded from a CDN for the jQuery UI widgets and have to be implemented
  26. in your pages stylesheet file or loaded from an extra stylesheet file. A default theme called Flora can be
  27. obtained from the jQuery UI downloadable file.</para>
  28. <example id="zend.jquery.view.jquery.usage">
  29. <title>jQuery() View Helper Example</title>
  30. <para>
  31. In this example a jQuery environment using the core and UI libraries will be needed. UI Widgets should
  32. be rendered with the Flora thema that is installed in 'public/styles/flora.all.css'. The jQuery libraries
  33. are both loaded from local paths.
  34. </para>
  35. <para>
  36. To register the jQuery functionality inside the view object, you have to add the appropriate helpers
  37. to the view helper path. There are many ways of accomplishing this, based on the requirements that the
  38. jQuery helpers have. If you need them in one specific view only, you can use the addHelperPath method
  39. on initialization of this view, or right before rendering:
  40. </para>
  41. <programlisting language="php"><![CDATA[
  42. $view->addHelperPath('ZendX/JQuery/View/Helper/', 'ZendX_JQuery_View_Helper');
  43. ]]></programlisting>
  44. <para>If you need them throughout your application, you can register them in your bootstrap
  45. file using access to the Controller Plugin ViewRenderer:
  46. </para>
  47. <programlisting language="php"><![CDATA[
  48. $view = new Zend_View();
  49. $view->addHelperPath('ZendX/JQuery/View/Helper/', 'ZendX_JQuery_View_Helper');
  50. $viewRenderer = new Zend_Controller_Action_Helper_ViewRenderer();
  51. $viewRenderer->setView($view);
  52. Zend_Controller_Action_HelperBroker::addHelper($viewRenderer);
  53. ]]></programlisting>
  54. <para>Now in the view script we want to display a Date Picker and an Ajax based Link.</para>
  55. <programlisting language="php"><![CDATA[
  56. <?php echo $this->ajaxLink("Show me something",
  57. "/hello/world",
  58. array('update' => '#content'));?>
  59. <div id="content"></div>
  60. <form method="post" action="/hello/world">
  61. Pick your Date: <?php echo $this->datePicker("dp1",
  62. '',
  63. array(
  64. 'defaultDate' =>
  65. date('Y/m/d', time())));?>
  66. <input type="submit" value="Submit" />
  67. </form>
  68. ]]></programlisting>
  69. <para>Both helpers now stacked some javascript statements on the jQuery helper and printed a link and
  70. a form element respectively. To access the javascript we have to utilize the jQuery()
  71. functionality. Both helpers already activated their dependencies that is they have called
  72. <methodname>jQuery()->enable()</methodname> and <methodname>jQuery()->uiEnable()</methodname>. We only have to print
  73. the jQuery() environment, and we choose to do so in the layout script's head segment:
  74. </para>
  75. <programlisting language="php"><![CDATA[
  76. <html>
  77. <head>
  78. <title>A jQuery View Helper Example</title>
  79. <?php echo $this->jQuery(); ?>
  80. </head>
  81. <body>
  82. <?php echo $this->layout()->content; ?>
  83. </body>
  84. </html>
  85. ]]></programlisting>
  86. <para>Although <code>$this->layout()->content;</code> is printed behind the <code>$this->jQuery()</code> statement,
  87. the content of the view script is rendered before. This way all the javascript onLoad code has already been put
  88. on the onLoad stack and can be printed within the head segment of the html document.
  89. </para>
  90. </example>
  91. <sect3 id="zendx.jquery.view.jquery.noconflict">
  92. <title>jQuery NoConflict Mode</title>
  93. <para>jQuery offers a noConflict mode that allows the library to be run side by side with other javascript libraries
  94. that operate in the global namespace, Prototype for example. The Zend Framework jQuery View Helper makes usage of
  95. the noConflict mode very easy. If you want to run Prototype and jQuery side by side you can call
  96. <code>ZendX_JQuery_View_Helper_JQuery::enableNoConflictMode();</code> and all jQuery helpers will operate in the No Conflict
  97. Mode.</para>
  98. <example id="zend.jquery.view.jquery.noconflict">
  99. <title>Building your own Helper with No Conflict Mode</title>
  100. <para>To make use of the NoConflict Mode in your own jQuery helper, you only have to use the static method
  101. <methodname>ZendX_JQuery_View_Helper_JQuery::getJQueryHandler()</methodname> method. It returns the variable jQuery is operating
  102. in at the moment, either <code>$</code> or <varname>$j</varname>
  103. </para>
  104. <programlisting language="php"><![CDATA[
  105. class MyHelper_SomeHelper extends Zend_View_Helper_Abstract
  106. {
  107. public function someHelper()
  108. {
  109. $jquery = $this->view->jQuery();
  110. $jquery->enable(); // enable jQuery Core Library
  111. // get current jQuery handler based on noConflict settings
  112. $jqHandler = ZendX_JQuery_View_Helper_JQuery::getJQueryHandler();
  113. $function = '("#element").click(function() '
  114. . '{ alert("noConflict Mode Save Helper!"); }'
  115. . ')';
  116. $jquery->addOnload($jqHandler . $function);
  117. return '';
  118. }
  119. }
  120. ]]></programlisting>
  121. </example>
  122. </sect3>
  123. <sect3 id="zendx.jquery.view.jquery.themes">
  124. <title>jQuery UI Themes</title>
  125. <para>Since there are no online available themes to use out of the box, the implementation of the UI library themes
  126. is a bit more complex than with the Dojo helper. The jQuery UI documentation describes for each component
  127. what stylesheet information is needed and the Default and Flora Themes from the downloadable archive give hints
  128. on the usage of stylesheets. The jQuery helper offers the function <code>jQuery()->addStylesheet($path);</code>
  129. function to include the dependant stylesheets whenever the helper is enabled and rendered. You can optionally
  130. merge the required stylesheet information in your main stylesheet file.
  131. </para>
  132. </sect3>
  133. <sect3 id="zendx.jquery.view.jquery.methods">
  134. <title>Methods Available</title>
  135. <para>
  136. The <methodname>jQuery()</methodname> view helper always returns an instance of
  137. the jQuery placeholder container. That container object has the
  138. following methods available:
  139. </para>
  140. <sect4 id="zendx.jquery.view.jquery.methods.core">
  141. <title>jQuery Core Library methods</title>
  142. <itemizedlist>
  143. <listitem><para><methodname>enable()</methodname>: explicitly enable jQuery
  144. integration.</para></listitem>
  145. <listitem><para><methodname>disable()</methodname>: disable jQuery
  146. integration.</para></listitem>
  147. <listitem><para><methodname>isEnabled()</methodname>: determine whether or not
  148. jQuery integration is enabled.</para></listitem>
  149. <listitem><para><methodname>setVersion()</methodname>: set the jQuery version
  150. that is used. This also decides on the library loaded
  151. from the Google Ajax Library CDN</para></listitem>
  152. <listitem><para><methodname>getVersion()</methodname>: get the current jQuery
  153. that is used. This also decides on the library loaded
  154. from the Google Ajax Library CDN</para></listitem>
  155. <listitem><para><methodname>useCdn()</methodname>: Return true, if CDN usage is
  156. currently enabled</para></listitem>
  157. <listitem><para><methodname>useLocalPath()</methodname>: Return true, if local usage
  158. is currently enabled</para></listitem>
  159. <listitem><para><methodname>setLocalPath()</methodname>: Set the local path to the
  160. jQuery Core library</para></listitem>
  161. <listitem><para><methodname>getLocalPath()</methodname>: If set, return the local path to the
  162. jQuery Core library</para></listitem>
  163. </itemizedlist>
  164. </sect4>
  165. <sect4 id="zendx.jquery.view.jquery.methods.ui">
  166. <title>jQuery UI Library methods</title>
  167. <itemizedlist>
  168. <listitem><para><methodname>uiEnable()</methodname>: explicitly enable jQuery UI
  169. integration.</para></listitem>
  170. <listitem><para><methodname>uiDisable()</methodname>: disable jQuery UI
  171. integration.</para></listitem>
  172. <listitem><para><methodname>uiIsEnabled()</methodname>: determine whether or not
  173. jQuery UI integration is enabled.</para></listitem>
  174. <listitem><para><methodname>setUiVersion()</methodname>: set the jQuery UI version
  175. that is used. This also decides on the library loaded
  176. from the Google Ajax Library CDN</para></listitem>
  177. <listitem><para><methodname>getUiVersion()</methodname>: get the current jQuery UI
  178. that is used. This also decides on the library loaded
  179. from the Google Ajax Library CDN</para></listitem>
  180. <listitem><para><methodname>useUiCdn()</methodname>: Return true, if CDN usage is
  181. currently enabled for jQuery UI</para></listitem>
  182. <listitem><para><methodname>useUiLocal()</methodname>: Return true, if local usage
  183. is currently enabled for jQuery UI</para></listitem>
  184. <listitem><para><methodname>setUiLocalPath()</methodname>: Set the local path to the
  185. jQuery UI library</para></listitem>
  186. <listitem><para><methodname>getUiLocalPath()</methodname>: If set, get the local path
  187. to the jQuery UI library</para></listitem>
  188. </itemizedlist>
  189. </sect4>
  190. <sect4 id="zendx.jquery.view.jquery.methods.other">
  191. <title>jQuery Helper Utility methods</title>
  192. <itemizedlist>
  193. <listitem><para><methodname>setView(Zend_View_Interface $view)</methodname>: set
  194. a view instance in the container.</para></listitem>
  195. <listitem><para><methodname>onLoadCaptureStart()</methodname>: Start capturing javascript code
  196. for jQuery onLoad execution.</para></listitem>
  197. <listitem><para><methodname>onLoadCaptureEnd()</methodname>: Stop capturing</para></listitem>
  198. <listitem><para><methodname>javascriptCaptureStart()</methodname>: Start capturing javascript code
  199. that has to be rendered after the inclusion of either jQuery Core or UI libraries.</para></listitem>
  200. <listitem><para><methodname>javascriptCaptureEnd()</methodname>: Stop capturing.</para></listitem>
  201. <listitem><para><methodname>addJavascriptFile($path)</methodname>: Add javascript file to be included after
  202. jQuery Core or UI library.</para></listitem>
  203. <listitem><para><methodname>getJavascriptFiles()</methodname>: Return all currently registered
  204. additional javascript files.</para></listitem>
  205. <listitem><para><methodname>clearJavascriptFiles()</methodname>: Clear the javascript files</para></listitem>
  206. <listitem><para><methodname>addJavascript($statement)</methodname>: Add javascript statement to be included
  207. after jQuery Core or UI library.</para></listitem>
  208. <listitem><para><methodname>getJavascript()</methodname>: Return all currently registered
  209. additional javascript statements.</para></listitem>
  210. <listitem><para><methodname>clearJavascript()</methodname>: Clear the javascript statements.</para></listitem>
  211. <listitem><para><methodname>addStylesheet($path)</methodname>: Add a stylesheet file that is needed
  212. for a jQuery view helper to display correctly.</para></listitem>
  213. <listitem><para><methodname>getStylesheets()</methodname>: Get all currently registered
  214. additional stylesheets.</para></listitem>
  215. <listitem><para><methodname>addOnLoad($statement)</methodname>: Add javascript statement that should
  216. be executed on document loading.</para></listitem>
  217. <listitem><para><methodname>getOnLoadActions()</methodname>: Return all currently registered
  218. onLoad statements.</para></listitem>
  219. <listitem><para><methodname>setRenderMode($mask)</methodname>: Render only a specific subset of the
  220. jQuery environment via ZendX_JQuery::RENDER_ constants. Rendering all elements
  221. is the default behaviour.</para></listitem>
  222. <listitem><para><methodname>getRenderMode()</methodname>: Return the current
  223. jQuery environment rendering mode.</para></listitem>
  224. <listitem><para><methodname>setCdnSsl($bool)</methodname>: Set if the CDN Google Ajax Library should be loaded
  225. from an SSL or a Non-SSL location.</para></listitem>
  226. </itemizedlist>
  227. <para>These are quite a number of methods, but many of them are used for internally by all the
  228. additional view helpers and during the printing of the jQuery environment. Unless you
  229. want to build your own jQuery helper or have a complex use-case, you will probably only
  230. get in contact with a few methods of these.</para>
  231. </sect4>
  232. </sect3>
  233. <sect3 id="zendx.jquery.view.jquery.refactor">
  234. <title>Refactoring jQuery environment with setRenderMode()</title>
  235. <para>Using the current setup that was described, each page of your website would show
  236. a different subset of jQuery code that would be needed to keep the current jQuery related
  237. items running. Also different files or stylesheets may be included depending on which
  238. helpers you implemented in your application. In production stage you might want to centralize
  239. all the javascript your application generated into a single file, or disable stylesheet
  240. rendering because you have merged all the stylesheets into a single file and include it statically
  241. in your layout. To allow a smooth refactoring you can enable or disable the rendering
  242. of certain jQuery environment blocks with help of the following constants and the
  243. <methodname>jQuery()->setRenderMode($bitmask)</methodname> function.</para>
  244. <itemizedlist>
  245. <listitem><para><code>ZendX_JQuery::RENDER_LIBRARY</code>: Renders jQuery Core and UI library</para></listitem>
  246. <listitem><para><code>ZendX_JQuery::RENDER_SOURCES</code>: Renders additional javascript files</para></listitem>
  247. <listitem><para><code>ZendX_JQuery::RENDER_STYLESHEETS</code>: Renders jQuery related stylesheets</para></listitem>
  248. <listitem><para><code>ZendX_JQuery::RENDER_JAVASCRIPT</code>: Render additional javascript statements</para></listitem>
  249. <listitem><para><code>ZendX_JQuery::RENDER_JQUERY_ON_LOAD</code>: Render jQuery onLoad statements</para></listitem>
  250. <listitem><para><code>ZendX_JQuery::RENDER_ALL</code>: Render all previously mentioned blocks, this is default behaviour.</para></listitem>
  251. </itemizedlist>
  252. <para>
  253. For an example, if you would have merged jQuery Core and UI libraries as well as other files into a single large file
  254. as well as merged stylesheets to keep HTTP requests low on your production application.
  255. You could disallow the jQuery helper to render those parts, but render all the other stuff
  256. with the following statement in your view:
  257. </para>
  258. <programlisting language="php"><![CDATA[
  259. $view->jQuery()
  260. ->setRenderMode(ZendX_JQuery::RENDER_JAVASCRIPT |
  261. ZendX_JQuery::RENDER_JQUERY_ON_LOAD);
  262. ]]></programlisting>
  263. <para>This statement makes sure only the required javascript statements and onLoad blocks of the current page are rendered by the jQuery helper.</para>
  264. </sect3>
  265. <sect3 id="zendx.jquery.view.jquery.migrations">
  266. <title>Migrations</title>
  267. <para>Prior to 1.8 the methods <methodname>setCdnVersion()</methodname>, <methodname>setLocalPath()</methodname>
  268. <methodname>setUiCdnVersion()</methodname> and <methodname>setUiLocalPath()</methodname> all enabled the view
  269. helper upon calling, which is considered a bug from the following perspective: If
  270. you want to use the any non-default library option, you would have to manually disable the
  271. jQuery helper aftwards if you only require it to be loaded in some scenarios. With version
  272. 1.8 the jQuery helper does only enable itsself, when <methodname>enable()</methodname> is called,
  273. which all internal jQuery View helpers do upon being called.
  274. </para>
  275. </sect3>
  276. </sect2>