2
0

Zend_Dojo-View-Dojo.xml 22 KB

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