Zend_Dojo-BuildLayers.xml 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- EN-Revision: 16720 -->
  3. <!-- Reviewed: no -->
  4. <sect1 id="zend.dojo.build-layers">
  5. <title>Support für den Build Layer von Zend_Dojo</title>
  6. <sect2 id="zend.dojo.build-layers.introduction">
  7. <title>Einführung</title>
  8. <para>
  9. Dojo Build Layer bieten einen reinen Pfad von der Entwicklung zur Produktion wenn Dojo
  10. für den eigenen UI Layer verwendet wird. In der Entwicklung kann man auf-Wunsch laden,
  11. und schnelles Anwendungs Prototyping erhalten; ein Build Layer nimmt alle Abhängigkeiten
  12. von Dojo und kompiliert diese in eine einzelne Datei, wobei optional Leerzeichen und
  13. Kommentare herausgenommen werden, führt Code Heuristiken durch um weitere
  14. Minimalisierungen von Variablennamen zu erlauben. Zusätzlich kann es auch
  15. <acronym>CSS</acronym> Minimalisierungen durchführen.
  16. </para>
  17. <para>
  18. Um einen Build Layer zu erstellen würde man traditioneller Weise eine JavaScript Datei
  19. erstellen die <code>dojo.require</code> Anweisungen für jede Abhängigkeit hat, und
  20. optional einigen zusätzlichen Code den man ausführen will wen das Skript geladen wird.
  21. Als Beispiel:
  22. </para>
  23. <programlisting language="javascript"><![CDATA[
  24. dojo.provide("custom.main");
  25. dojo.require("dijit.layout.TabContainer");
  26. dojo.require("dijit.layout.ContentPane");
  27. dojo.require("dijit.form.Form");
  28. dojo.require("dijit.form.Button");
  29. dojo.require("dijit.form.TextBox");
  30. ]]></programlisting>
  31. <para>
  32. Auf dieses Skript wird generell als "layer" Skript referiert.
  33. </para>
  34. <para>
  35. Im eigenen Anwendungs Layer, würde man dann Dojo instruieren dieses Modul zu laden:
  36. </para>
  37. <programlisting language="html"><![CDATA[
  38. <html>
  39. <head>
  40. <script type="text/javascript" src="/js/dojo/dojo.js"></script>
  41. <script type="text/javascript">
  42. dojo.registerModulePath("custom", "../custom/");
  43. dojo.require("custom.main");
  44. </script>
  45. ]]></programlisting>
  46. <para>
  47. Wenn man <classname>Zend_Dojo</classname> verwendet um das zu tun, würde man das
  48. folgende durchführen:
  49. </para>
  50. <programlisting language="php"><![CDATA[
  51. $view->dojo()->registerModulePath('custom', '../custom/')
  52. ->requireModule('custom.main');
  53. ]]></programlisting>
  54. <para>
  55. Aber da <classname>Zend_Dojo</classname> die verschiedenen <code>dojo.require</code>
  56. Anweisungen zusammenfügt, wie kann man das eigene Layer Skript erstellen? Man könnte
  57. jede Seite öffnen, die erstellten <code>dojo.require</code> Anweisungen anschauen und
  58. Sie herausschneiden und in eine Layer Skript Datei manuell einfügen.
  59. </para>
  60. <para>
  61. Trotzdem existiert eine bessere Lösung: Da <classname>Zend_Dojo</classname> diese
  62. Informationen bereits zusammenfügt, kann man diese Information einfach herausziehen und
  63. die eigene Layer Datei erstellen. Das ist der Sinn von
  64. <classname>Zend_Dojo_BuildLayer</classname>.
  65. </para>
  66. </sect2>
  67. <sect2 id="zend.dojo.build-layers.usage">
  68. <title>Erstellen eigener Modul Layer mit Zend_Dojo_BuildLayer</title>
  69. <para>
  70. Am einfachsten kann man einfach <classname>Zend_Dojo_BuildLayer</classname>
  71. instanziieren, es dem View Objekt zusammen mit dem Namen des eigenen Modul Layers
  72. füttern, und es den Inhalt der Layer Datei erstellen lassen; es liegt an einem selbst
  73. diese anschließend auf die Festplatte zu schreiben.
  74. </para>
  75. <para>
  76. Nehmen wir als Beispiel an das man den Modul Layer "custom.main" erstellen will.
  77. Angenommen man folgt der vorgeschlagenen Projekt Verzeichnisstruktur, und man
  78. will JavaScript Dateien unter <filename>public/js/</filename> speichern, dann
  79. könnte man das folgende tun:
  80. </para>
  81. <programlisting language="php"><![CDATA[
  82. $build = new Zend_Dojo_BuildLayer(array(
  83. 'view' => $view,
  84. 'layerName' => 'custom.main',
  85. ));
  86. $layerContents = $build->generateLayerScript();
  87. $filename = APPLICATION_PATH . '/../public/js/custom/main.js';
  88. if (!dir_exists(dirname($filename))) {
  89. mkdir(dirname($filename));
  90. }
  91. file_put_contents($filename, $layerContents);
  92. ]]></programlisting>
  93. <para>
  94. Wann sollte man das obige durchführen? Damit es korrekt arbeitet, muß man es nach
  95. der Darstellung aller View Skripte und des Layouts tun um sicherzustellen das der
  96. <methodname>dojo()</methodname> Helfer vollständig bestückt wurde. Ein einfacher
  97. Weg um das zu tun ist die Verwendung des Front Controller Plugins, mit einem
  98. <methodname>dispatchLoopShutdown()</methodname> Hook:
  99. </para>
  100. <programlisting language="php"><![CDATA[
  101. class App_Plugin_DojoLayer extends Zend_Controller_Plugin_Abstract
  102. {
  103. public $layerScript = APPLICATION_PATH . '/../public/js/custom/main.js';
  104. protected $_build;
  105. public function dispatchLoopShutdown()
  106. {
  107. if (!file_exists($this->layerScript)) {
  108. $this->generateDojoLayer();
  109. }
  110. }
  111. public function getBuild()
  112. {
  113. if (null === $this->_build) {
  114. $this->_build = new Zend_Dojo_BuildLayer(array(
  115. 'view' => $view,
  116. 'layerName' => 'custom.main',
  117. ));
  118. }
  119. return $this->_build;
  120. }
  121. public function generateDojoLayer()
  122. {
  123. $build = $this->getBuild();
  124. $layerContents = $build->generateLayerScript();
  125. if (!dir_exists(dirname($this->layerScript))) {
  126. mkdir(dirname($this->layerScript));
  127. }
  128. file_put_contents($this->layerScript, $layerContents);
  129. }
  130. }
  131. ]]></programlisting>
  132. <note>
  133. <title>Den Layer nicht in jeder Seite erstellen</title>
  134. <para>
  135. Es ist verführerisch das Layer Skript auf jeder einzelnen Seite zu erstellen.
  136. Aber das ist Ressourcen intensiv da hierbei für jede Seite auf die Festplatte
  137. geschrieben werden muß. Zusätzlich erhält man keine Vorteile von Client seitigem
  138. Cachen, da mtime von der Datei sich jedesmal ändert. Die Datei sollte nur
  139. einmal geschrieben werden.
  140. </para>
  141. </note>
  142. <sect3 id="zend.dojo.build-layers.usage.options">
  143. <title>BuildLayer options</title>
  144. <para>
  145. The above functionality will suffice for most situations. For
  146. those needing more customization, a variety of options may be
  147. invoked.
  148. </para>
  149. <sect4 id="zend.dojo.build-layers.usage.options.view">
  150. <title>Setting the view object</title>
  151. <para>
  152. While the view object may be passed during instantiation,
  153. you may also pass it in to an instance via the
  154. <methodname>setView()</methodname> method:
  155. </para>
  156. <programlisting language="php"><![CDATA[
  157. $build->setView($view);
  158. ]]></programlisting>
  159. </sect4>
  160. <sect4 id="zend.dojo.build-layers.usage.options.layername">
  161. <title>Setting the layer name</title>
  162. <para>
  163. While the layer name may be passed during instantiation,
  164. you may also pass it in to an instance via the
  165. <methodname>setLayerName()</methodname> method:
  166. </para>
  167. <programlisting language="php"><![CDATA[
  168. $build->setLayerName("custom.main");
  169. ]]></programlisting>
  170. </sect4>
  171. <sect4 id="zend.dojo.build-layers.usage.options.onload">
  172. <title>Including onLoad events in the generated layer</title>
  173. <para>
  174. <code>dojo.addOnLoad</code> is a useful utility for
  175. specifying actions that should trigger when the <acronym>DOM</acronym> has
  176. finished loading. The <methodname>dojo()</methodname> view helper can
  177. create these statements via its
  178. <methodname>addOnLoad()</methodname> and
  179. <methodname>onLoadCapture*()</methodname> methods. In some
  180. cases, it makes sense to push these into your layer file
  181. instead of rendering them via your view scripts.
  182. </para>
  183. <para>
  184. By default, these are not rendered; to enable them, pass the
  185. <property>consumeOnLoad</property> configuration key during
  186. instantiation:
  187. </para>
  188. <programlisting language="php"><![CDATA[
  189. $build = new Zend_Dojo_BuildLayer(array(
  190. 'view' => $view,
  191. 'layerName' => 'custom.main',
  192. 'consumeOnLoad' => true,
  193. ));
  194. ]]></programlisting>
  195. <para>
  196. Alternately, you can use the
  197. <methodname>setConsumeOnLoad()</methodname> method after
  198. instantiation:
  199. </para>
  200. <programlisting language="php"><![CDATA[
  201. $build->setConsumeOnLoad(true);
  202. ]]></programlisting>
  203. </sect4>
  204. <sect4 id="zend.dojo.build-layers.usage.options.javascript">
  205. <title>Including captured JavaScript in the generated layer</title>
  206. <para>
  207. The <methodname>dojo()</methodname> view helper includes methods for
  208. capturing arbitrary JavaScript to include in the
  209. &lt;script&gt; tag containing the various
  210. <code>dojo.require</code> and <code>dojo.addOnLoad</code>
  211. statements. This can be useful when creating default data
  212. stores or globally scoped objects used throughout your
  213. application.
  214. </para>
  215. <para>
  216. By default, these are not rendered; to enable them, pass the
  217. <property>consumeJavascript</property> configuration key during
  218. instantiation:
  219. </para>
  220. <programlisting language="php"><![CDATA[
  221. $build = new Zend_Dojo_BuildLayer(array(
  222. 'view' => $view,
  223. 'layerName' => 'custom.main',
  224. 'consumeJavascript' => true,
  225. ));
  226. ]]></programlisting>
  227. <para>
  228. Alternately, you can use the
  229. <methodname>setConsumeJavascript()</methodname> method after
  230. instantiation:
  231. </para>
  232. <programlisting language="php"><![CDATA[
  233. $build->setConsumeJavascript(true);
  234. ]]></programlisting>
  235. </sect4>
  236. </sect3>
  237. </sect2>
  238. <sect2 id="zend.dojo.build-layers.profiles">
  239. <title>Generating Build Profiles with Zend_Dojo_BuildLayer</title>
  240. <para>
  241. One of the chief benefits of a Dojo module layer is that it
  242. facilitates the creation of a custom build.
  243. <classname>Zend_Dojo_BuildLayer</classname> has functionality for
  244. generate build profiles.
  245. </para>
  246. <para>
  247. The simplest use case is to utilize the
  248. <methodname>generateBuildProfile()</methodname> method and send the
  249. output to a file:
  250. </para>
  251. <programlisting language="php"><![CDATA[
  252. $build = new Zend_Dojo_BuildLayer(array(
  253. 'view' => $view,
  254. 'layerName' => 'custom.main',
  255. ));
  256. $profile = $build->generateBuildProfile();
  257. $filename = APPLICATION_PATH . '/../misc/scripts/custom.profile.js';
  258. file_put_contents($filename, $profile);
  259. ]]></programlisting>
  260. <para>
  261. Just like generating layers, you may want to automate this via a
  262. <methodname>dispatchLoopShutdown()</methodname> plugin hook; you
  263. could even simply modify the one shown for generating layers to read
  264. as follows:
  265. </para>
  266. <programlisting language="php"><![CDATA[
  267. class App_Plugin_DojoLayer extends Zend_Controller_Plugin_Abstract
  268. {
  269. public $layerScript = APPLICATION_PATH
  270. . '/../public/js/custom/main.js';
  271. public $buildProfile = APPLICATION_PATH
  272. . '/../misc/scripts/custom.profile.js';
  273. protected $_build;
  274. public function dispatchLoopShutdown()
  275. {
  276. if (!file_exists($this->layerScript)) {
  277. $this->generateDojoLayer();
  278. }
  279. if (!file_exists($this->buildProfile)) {
  280. $this->generateBuildProfile();
  281. }
  282. }
  283. public function generateDojoLayer() { /* ... */ }
  284. public function generateBuildProfile()
  285. {
  286. $profile = $this->getBuild()->generateBuildProfile();
  287. file_put_contents($this->buildProfile, $profile);
  288. }
  289. }
  290. ]]></programlisting>
  291. <para>
  292. As noted, with module layers, you should only create the file once.
  293. </para>
  294. <sect3 id="zend.dojo.build-layers.profiles.options">
  295. <title>Build Profile options</title>
  296. <para>
  297. The above functionality will suffice for most situations. The
  298. only way to customize build profile generation is to provide
  299. additional build profile options to utilize.
  300. </para>
  301. <para>
  302. As an example, you may want to specify what type of
  303. optimizations should be performed, whether or not to optimize
  304. <acronym>CSS</acronym> files in the layer, whether or not to copy tests into the
  305. build, etc. For a listing of available options, you should read
  306. the <ulink url="http://docs.dojocampus.org/build/index">Dojo
  307. Build documentation</ulink> and <ulink
  308. url="http://www.dojotoolkit.org/book/dojo-book-0-9/part-4-meta-dojo/package-system-and-custom-builds">accompanying
  309. documentation</ulink>.
  310. </para>
  311. <para>
  312. Setting these options is trivial: use the
  313. <methodname>addProfileOption()</methodname>,
  314. <methodname>addProfileOptions()</methodname>, or
  315. <methodname>setProfileOptions()</methodname> methods. The first
  316. method adds a single key and value option pair, the second will add
  317. several, and the third will overwrite any options with the list
  318. of key and value pairs provided.
  319. </para>
  320. <para>
  321. By default, the following options are set:
  322. </para>
  323. <programlisting language="javascript"><![CDATA[
  324. {
  325. action: "release",
  326. optimize: "shrinksafe",
  327. layerOptimize: "shrinksafe",
  328. copyTests: false,
  329. loader: "default",
  330. cssOptimize: "comments"
  331. }
  332. ]]></programlisting>
  333. <para>
  334. You can pass in whatever key and value pairs you want; the Dojo
  335. build script will ignore those it does not understand.
  336. </para>
  337. <para>
  338. As an example of setting options:
  339. </para>
  340. <programlisting language="php"><![CDATA[
  341. // A single option:
  342. $build->addProfileOption('version', 'zend-1.3.1');
  343. // Several options:
  344. $build->addProfileOptions(array(
  345. 'loader' => 'xdomain',
  346. 'optimize' => 'packer',
  347. ));
  348. // Or overwrite options:
  349. $build->setProfileOptions(array(
  350. 'version' => 'custom-1.3.1',
  351. 'loader' => 'shrinksafe',
  352. 'optimize' => 'shrinksafe',
  353. ));
  354. ]]></programlisting>
  355. </sect3>
  356. </sect2>
  357. </sect1>