Zend_Dojo-View-Dojo.xml 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect2 id="zend.dojo.view.dojo">
  4. <title>dojo() View Helper</title>
  5. <para>
  6. The <methodname>dojo()</methodname> view helper is intended to simplify setting up
  7. the Dojo environment, including the following responsibilities:
  8. </para>
  9. <itemizedlist>
  10. <listitem><para>Specifying either a CDN or a local path to a Dojo
  11. install.</para></listitem>
  12. <listitem><para>Specifying paths to custom Dojo modules.</para></listitem>
  13. <listitem><para>Specifying dojo.require statements.</para></listitem>
  14. <listitem><para>Specifying dijit stylesheet themes to use.</para></listitem>
  15. <listitem><para>Specifying dojo.addOnLoad() events.</para></listitem>
  16. </itemizedlist>
  17. <para>
  18. The <methodname>dojo()</methodname> view helper implementation is an example of a
  19. placeholder implementation. The data set in it persists between view
  20. objects and may be directly echoed from your layout script.
  21. </para>
  22. <example id="zend.dojo.view.dojo.usage">
  23. <title>dojo() View Helper Usage Example</title>
  24. <para>
  25. For this example, let's assume the developer will be using Dojo from
  26. a local path, will require several dijits, and will be
  27. utilizing the Tundra dijit theme.
  28. </para>
  29. <para>
  30. On many pages, the developer may not utilize Dojo at all. So, we
  31. will first focus on a view script where Dojo is needed and then on the
  32. layout script, where we will setup some of the Dojo environment and
  33. then render it.
  34. </para>
  35. <para>
  36. First, we need to tell our view object to use the Dojo view helper
  37. paths. This can be done in your bootstrap or an early-running
  38. plugin; simply grab your view object and execute the following:
  39. </para>
  40. <programlisting language="php"><![CDATA[
  41. $view->addHelperPath('Zend/Dojo/View/Helper/', 'Zend_Dojo_View_Helper');
  42. ]]></programlisting>
  43. <para>
  44. Next, the view script. In this case, we're going to specify
  45. that we will be using a FilteringSelect -- which will consume a
  46. custom store based on QueryReadStore, which we'll call
  47. 'PairedStore' and store in our 'custom' module.
  48. </para>
  49. <programlisting language="php"><![CDATA[
  50. <?php // setup data store for FilteringSelect ?>
  51. <div dojoType="custom.PairedStore" jsId="stateStore"
  52. url="/data/autocomplete/type/state/format/ajax"
  53. requestMethod="get"></div>
  54. <?php // Input element: ?>
  55. State: <input id="state" dojoType="dijit.form.FilteringSelect"
  56. store="stateStore" pageSize="5" />
  57. <?php // setup required dojo elements:
  58. $this->dojo()->enable()
  59. ->setDjConfigOption('parseOnLoad', true)
  60. ->registerModulePath('custom', '../custom/')
  61. ->requireModule('dijit.form.FilteringSelect')
  62. ->requireModule('custom.PairedStore'); ?>
  63. ]]></programlisting>
  64. <para>
  65. In our layout script, we'll then check to see if Dojo is enabled,
  66. and, if so, we'll do some more general configuration and assemble
  67. it:
  68. </para>
  69. <programlisting language="php"><![CDATA[
  70. <?php echo $this->doctype() ?>
  71. <html>
  72. <head>
  73. <?php echo $this->headTitle() ?>
  74. <?php echo $this->headMeta() ?>
  75. <?php echo $this->headLink() ?>
  76. <?php echo $this->headStyle() ?>
  77. <?php if ($this->dojo()->isEnabled()){
  78. $this->dojo()->setLocalPath('/js/dojo/dojo.js')
  79. ->addStyleSheetModule('dijit.themes.tundra');
  80. echo $this->dojo();
  81. }
  82. ?>
  83. <?php echo $this->headScript() ?>
  84. </head>
  85. <body class="tundra">
  86. <?php echo $this->layout()->content ?>
  87. <?php echo $this->inlineScript() ?>
  88. </body>
  89. </html>
  90. ]]></programlisting>
  91. <para>
  92. At this point, you only need to ensure that your files are in the
  93. correct locations and that you've created the end point action for
  94. your FilteringSelect!
  95. </para>
  96. </example>
  97. <note>
  98. <title>UTF-8 encoding used by default</title>
  99. <para>
  100. By default, Zend Framework uses <acronym>UTF-8</acronym> as its default encoding, and,
  101. specific to this case, <classname>Zend_View</classname> does as well. Character encoding
  102. can be set differently on the view object itself using the
  103. <methodname>setEncoding()</methodname> method (or the the <varname>encoding</varname>
  104. instantiation parameter). However, since <classname>Zend_View_Interface</classname> does
  105. not define accessors for encoding, it's possible that if you are using a custom view
  106. implementation with the Dojo view helper, you will not have a
  107. <methodname>getEncoding()</methodname> method, which is what the view helper uses
  108. internally for determining the character set in which to encode.
  109. </para>
  110. <para>
  111. If you do not want to utilize <acronym>UTF-8</acronym> in such a situation, you will
  112. need to implement a <methodname>getEncoding()</methodname> method in your custom view
  113. implementation.
  114. </para>
  115. </note>
  116. <sect3 id="zend.dojo.view.dojo.declarative">
  117. <title>Programmatic and Declarative Usage of Dojo</title>
  118. <para>
  119. Dojo allows both <emphasis>declarative</emphasis> and
  120. <emphasis>programmatic</emphasis> usage of many of its features.
  121. <emphasis>Declarative</emphasis> usage uses standard HTML elements
  122. with non-standard attributes that are parsed when the page is
  123. loaded. While this is a powerful and simple syntax to utilize, for
  124. many developers this can cause issues with page validation.
  125. </para>
  126. <para>
  127. <emphasis>Programmatic</emphasis> usage allows the developer to
  128. decorate existing elements by pulling them by ID or <acronym>CSS</acronym> selectors
  129. and passing them to the appropriate object constructors in Dojo.
  130. Because no non-standard HTML attributes are used, pages continue to
  131. validate.
  132. </para>
  133. <para>
  134. In practice, both use cases allow for graceful degradation when
  135. javascript is disabled or the various Dojo script resources are
  136. unreachable. To promote standards and document validation,
  137. Zend Framework uses programmatic usage by default; the various view
  138. helpers will generate javascript and push it to the
  139. <methodname>dojo()</methodname> view helper for inclusion when rendered.
  140. </para>
  141. <para>
  142. Developers using this technique may also wish to explore the option
  143. of writing their own programmatic decoration of the page. One
  144. benefit would be the ability to specify handlers for dijit events.
  145. </para>
  146. <para>
  147. To allow this, as well as the ability to use declarative syntax,
  148. there are a number of static methods available to set this behavior
  149. globally.
  150. </para>
  151. <example id="zend.dojo.view.dojo.declarative.usage">
  152. <title>Specifying Declarative and Programmatic Dojo Usage</title>
  153. <para>
  154. To specify declarative usage, simply call the static
  155. <methodname>setUseDeclarative()</methodname> method:
  156. </para>
  157. <programlisting language="php"><![CDATA[
  158. Zend_Dojo_View_Helper_Dojo::setUseDeclarative();
  159. ]]></programlisting>
  160. <para>
  161. If you decide instead to use programmatic usage, call the static
  162. <methodname>setUseProgrammatic()</methodname> method:
  163. </para>
  164. <programlisting language="php"><![CDATA[
  165. Zend_Dojo_View_Helper_Dojo::setUseProgrammatic();
  166. ]]></programlisting>
  167. <para>
  168. Finally, if you want to create your own programmatic rules, you
  169. should specify programmatic usage, but pass in the value '-1';
  170. in this situation, no javascript for decorating any dijits used
  171. will be created.
  172. </para>
  173. <programlisting language="php"><![CDATA[
  174. Zend_Dojo_View_Helper_Dojo::setUseProgrammatic(-1);
  175. ]]></programlisting>
  176. </example>
  177. </sect3>
  178. <sect3 id="zend.dojo.view.dojo.themes">
  179. <title>Themes</title>
  180. <para>
  181. Dojo allows the creation of themes for its dijits (widgets). You may
  182. select one by passing in a module path:
  183. </para>
  184. <programlisting language="php"><![CDATA[
  185. $view->dojo()->addStylesheetModule('dijit.themes.tundra');
  186. ]]></programlisting>
  187. <para>
  188. The module path is discovered by using the character '.' as a
  189. directory separator and using the last value in the list as the name
  190. of the <acronym>CSS</acronym> file in that theme directory to use; in the example
  191. above, Dojo will look for the theme in
  192. 'dijit/themes/tundra/tundra.css'.
  193. </para>
  194. <para>
  195. When using a theme, it is important to remember to pass the theme
  196. class to, at the least, a container surrounding any dijits you are
  197. using; the most common use case is to pass it in the body:
  198. </para>
  199. <programlisting language="html"><![CDATA[
  200. <body class="tundra">
  201. ]]></programlisting>
  202. </sect3>
  203. <sect3 id="zend.dojo.view.dojo.layers">
  204. <title>Using Layers (Custom Builds)</title>
  205. <para>
  206. By default, when you use a dojo.require statement, dojo will make a
  207. request back to the server to grab the appropriate javascript file.
  208. If you have many dijits in place, this results in many requests to
  209. the server -- which is not optimal.
  210. </para>
  211. <para>
  212. Dojo's answer to this is to provide the ability to create
  213. <emphasis>custom builds</emphasis>. Builds do several things:
  214. </para>
  215. <itemizedlist>
  216. <listitem><para>
  217. Groups required files into <emphasis>layers</emphasis>; a layer
  218. lumps all required files into a single JS file. (Hence the name
  219. of this section.)
  220. </para></listitem>
  221. <listitem><para>
  222. "Interns" non-javascript files used by dijits (typically,
  223. template files). These are also grouped in the same JS file as
  224. the layer.
  225. </para></listitem>
  226. <listitem><para>
  227. Passes the file through ShrinkSafe, which strips whitespace and
  228. comments, as well as shortens variable names.
  229. </para></listitem>
  230. </itemizedlist>
  231. <para>
  232. Some files can not be layered, but the build process will create a
  233. special release directory with the layer file and all other files.
  234. This allows you to have a slimmed-down distribution customized for
  235. your site or application needs.
  236. </para>
  237. <para>
  238. To use a layer, the <methodname>dojo()</methodname> view helper has a
  239. <methodname>addLayer()</methodname> method for adding paths to required layers:
  240. </para>
  241. <programlisting language="html"><![CDATA[
  242. $view->dojo()->addLayer('/js/foo/foo.js');
  243. ]]></programlisting>
  244. <para>
  245. For more information on creating custom builds, please <ulink
  246. url="http://dojotoolkit.org/book/dojo-book-0-9/part-4-meta-dojo/package-system-and-custom-builds">refer
  247. to the Dojo build documentation</ulink>.
  248. </para>
  249. </sect3>
  250. <sect3 id="zend.dojo.view.dojo.methods">
  251. <title>Methods Available</title>
  252. <para>
  253. The <methodname>dojo()</methodname> view helper always returns an instance of
  254. the dojo placeholder container. That container object has the
  255. following methods available:
  256. </para>
  257. <itemizedlist>
  258. <listitem><para><methodname>setView(Zend_View_Interface $view)</methodname>: set
  259. a view instance in the container.</para></listitem>
  260. <listitem><para><methodname>enable()</methodname>: explicitly enable Dojo
  261. integration.</para></listitem>
  262. <listitem><para><methodname>disable()</methodname>: disable Dojo
  263. integration.</para></listitem>
  264. <listitem><para><methodname>isEnabled()</methodname>: determine whether or not
  265. Dojo integration is enabled.</para></listitem>
  266. <listitem><para><methodname>requireModule($module)</methodname>: setup a
  267. <property>dojo.require</property> statement.</para></listitem>
  268. <listitem><para><methodname>getModules()</methodname>: determine what modules
  269. have been required.</para></listitem>
  270. <listitem><para><methodname>registerModulePath($module, $path)</methodname>:
  271. register a custom Dojo module path.</para></listitem>
  272. <listitem><para><methodname>getModulePaths()</methodname>: get list of
  273. registered module paths.</para></listitem>
  274. <listitem><para><methodname>addLayer($path)</methodname>: add a layer (custom
  275. build) path to use.</para></listitem>
  276. <listitem><para><methodname>getLayers()</methodname>: get a list of all
  277. registered layer paths (custom builds).</para></listitem>
  278. <listitem><para><methodname>removeLayer($path)</methodname>: remove the layer
  279. that matches <varname>$path</varname> from the list of registered
  280. layers (custom builds).</para></listitem>
  281. <listitem>
  282. <para>
  283. <methodname>setCdnBase($url)</methodname>: set the base <acronym>URL</acronym>
  284. for a CDN; typically, one of the <constant>Zend_Dojo::CDN_BASE_AOL</constant> or
  285. <constant>Zend_Dojo::CDN_BASE_GOOGLE</constant>, but it only needs
  286. to be the <acronym>URL</acronym> string prior to the version number.
  287. </para>
  288. </listitem>
  289. <listitem><para><methodname>getCdnBase()</methodname>: retrieve the base CDN url
  290. to utilize.</para></listitem>
  291. <listitem><para><methodname>setCdnVersion($version = null)</methodname>: set
  292. which version of Dojo to utilize from the CDN.</para></listitem>
  293. <listitem><para><methodname>getCdnVersion()</methodname>: retrieve what
  294. version of Dojo from the CDN will be used.</para></listitem>
  295. <listitem><para><methodname>setCdnDojoPath($path)</methodname>: set the relative
  296. path to the dojo.js or dojo.xd.js file on a CDN; typically,
  297. one of the <constant>Zend_Dojo::CDN_DOJO_PATH_AOL</constant> or
  298. <constant>Zend_Dojo::CDN_DOJO_PATH_GOOGLE</constant>, but it only
  299. needs to be the path string following the version
  300. number.</para></listitem>
  301. <listitem><para><methodname>getCdnDojoPath()</methodname>: retrieve the last
  302. path segment of the CDN url pointing to the dojo.js
  303. file.</para></listitem>
  304. <listitem><para><methodname>useCdn()</methodname>: tell the container to
  305. utilize the CDN; implicitly enables integration.</para></listitem>
  306. <listitem><para><methodname>setLocalPath($path)</methodname>: tell the container
  307. the path to a local Dojo install (should be a path relative
  308. to the server, and contain the dojo.js file itself);
  309. implicitly enables integration.</para></listitem>
  310. <listitem><para><methodname>getLocalPath()</methodname>: determine what local
  311. path to Dojo is being used.</para></listitem>
  312. <listitem><para><methodname>useLocalPath()</methodname>: is the integration
  313. utilizing a Dojo local path?</para></listitem>
  314. <listitem><para><methodname>setDjConfig(array $config)</methodname>: set
  315. dojo/dijit configuration values (expects assoc
  316. array).</para></listitem>
  317. <listitem><para><methodname>setDjConfigOption($option, $value)</methodname>: set
  318. a single dojo/dijit configuration value.</para></listitem>
  319. <listitem><para><methodname>getDjConfig()</methodname>: get all dojo/dijit
  320. configuration values.</para></listitem>
  321. <listitem><para><methodname>getDjConfigOption($option, $default =
  322. null)</methodname>: get a single dojo/dijit configuration
  323. value.</para></listitem>
  324. <listitem><para><methodname>addStylesheetModule($module)</methodname>: add a
  325. stylesheet based on a module theme.</para></listitem>
  326. <listitem><para><methodname>getStylesheetModules()</methodname>: get stylesheets
  327. registered as module themes.</para></listitem>
  328. <listitem><para><methodname>addStylesheet($path)</methodname>: add a local
  329. stylesheet for use with Dojo.</para></listitem>
  330. <listitem><para><methodname>getStylesheets()</methodname>: get local Dojo
  331. stylesheets.</para></listitem>
  332. <listitem><para><methodname>addOnLoad($spec, $function = null)</methodname>: add
  333. a lambda for dojo.onLoad to call. If one argument is passed,
  334. it is assumed to be either a function name or a javascript
  335. closure. If two arguments are passed, the first is assumed
  336. to be the name of an object instance variable and the second
  337. either a method name in that object or a closure to utilize
  338. with that object.</para></listitem>
  339. <listitem><para><methodname>prependOnLoad($spec, $function = null)</methodname>:
  340. exactly like <methodname>addOnLoad()</methodname>, excepts prepends to
  341. beginning of onLoad stack.</para></listitem>
  342. <listitem><para><methodname>getOnLoadActions()</methodname>: retrieve all
  343. dojo.onLoad actions registered with the container. This will
  344. be an array of arrays.</para></listitem>
  345. <listitem><para><methodname>onLoadCaptureStart($obj = null)</methodname>:
  346. capture data to be used as a lambda for dojo.onLoad(). If
  347. $obj is provided, the captured JS code will be considered a
  348. closure to use with that Javascript object.</para></listitem>
  349. <listitem><para><methodname>onLoadCaptureEnd($obj = null)</methodname>: finish
  350. capturing data for use with dojo.onLoad().</para></listitem>
  351. <listitem><para><methodname>javascriptCaptureStart()</methodname>:
  352. capture arbitrary javascript to be included with Dojo JS
  353. (onLoad, require, etc. statements).</para></listitem>
  354. <listitem><para><methodname>javascriptCaptureEnd()</methodname>: finish
  355. capturing javascript.</para></listitem>
  356. <listitem><para><methodname>__toString()</methodname>: cast the container to a
  357. string; renders all HTML style and script elements.</para></listitem>
  358. </itemizedlist>
  359. </sect3>
  360. </sect2>
  361. <!--
  362. vim:se ts=4 sw=4 et:
  363. -->