Zend_Dojo-BuildLayers.xml 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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>Optionen für BuildLayer</title>
  144. <para>
  145. Die obige Funktionalität wird in den meisten Situationen ausreichen. Für jene die
  146. weitere Anpassungen benötigen, können eine Vielzahl von Optionen verwendet werden.
  147. </para>
  148. <sect4 id="zend.dojo.build-layers.usage.options.view">
  149. <title>Setzen des View Objekts</title>
  150. <para>
  151. Wärend das View Objekt wärend der Instanzierung übergeben werden kann, kann es
  152. einer Instanz auch über die <methodname>setView()</methodname> Methode
  153. übergeben werden:
  154. </para>
  155. <programlisting language="php"><![CDATA[
  156. $build->setView($view);
  157. ]]></programlisting>
  158. </sect4>
  159. <sect4 id="zend.dojo.build-layers.usage.options.layername">
  160. <title>Setzen des Namen eines Layers</title>
  161. <para>
  162. Wärend der Name des Layers wärend Instanzierung übergeben werden kann, kann er
  163. der Instanz auch über die <methodname>setLayerName()</methodname> Methode
  164. übergeben werden:
  165. </para>
  166. <programlisting language="php"><![CDATA[
  167. $build->setLayerName("custom.main");
  168. ]]></programlisting>
  169. </sect4>
  170. <sect4 id="zend.dojo.build-layers.usage.options.onload">
  171. <title>Einfügen von onLoad Events im erstellten Layer</title>
  172. <para>
  173. <code>dojo.addOnLoad</code> is a useful utility for
  174. specifying actions that should trigger when the <acronym>DOM</acronym> has
  175. finished loading. The <methodname>dojo()</methodname> view helper can
  176. create these statements via its
  177. <methodname>addOnLoad()</methodname> and
  178. <methodname>onLoadCapture*()</methodname> methods. In some
  179. cases, it makes sense to push these into your layer file
  180. instead of rendering them via your view scripts.
  181. </para>
  182. <para>
  183. By default, these are not rendered; to enable them, pass the
  184. <property>consumeOnLoad</property> configuration key during
  185. instantiation:
  186. </para>
  187. <programlisting language="php"><![CDATA[
  188. $build = new Zend_Dojo_BuildLayer(array(
  189. 'view' => $view,
  190. 'layerName' => 'custom.main',
  191. 'consumeOnLoad' => true,
  192. ));
  193. ]]></programlisting>
  194. <para>
  195. Alternately, you can use the
  196. <methodname>setConsumeOnLoad()</methodname> method after
  197. instantiation:
  198. </para>
  199. <programlisting language="php"><![CDATA[
  200. $build->setConsumeOnLoad(true);
  201. ]]></programlisting>
  202. </sect4>
  203. <sect4 id="zend.dojo.build-layers.usage.options.javascript">
  204. <title>Including captured JavaScript in the generated layer</title>
  205. <para>
  206. The <methodname>dojo()</methodname> view helper includes methods for
  207. capturing arbitrary JavaScript to include in the
  208. &lt;script&gt; tag containing the various
  209. <code>dojo.require</code> and <code>dojo.addOnLoad</code>
  210. statements. This can be useful when creating default data
  211. stores or globally scoped objects used throughout your
  212. application.
  213. </para>
  214. <para>
  215. By default, these are not rendered; to enable them, pass the
  216. <property>consumeJavascript</property> configuration key during
  217. instantiation:
  218. </para>
  219. <programlisting language="php"><![CDATA[
  220. $build = new Zend_Dojo_BuildLayer(array(
  221. 'view' => $view,
  222. 'layerName' => 'custom.main',
  223. 'consumeJavascript' => true,
  224. ));
  225. ]]></programlisting>
  226. <para>
  227. Alternately, you can use the
  228. <methodname>setConsumeJavascript()</methodname> method after
  229. instantiation:
  230. </para>
  231. <programlisting language="php"><![CDATA[
  232. $build->setConsumeJavascript(true);
  233. ]]></programlisting>
  234. </sect4>
  235. </sect3>
  236. </sect2>
  237. <sect2 id="zend.dojo.build-layers.profiles">
  238. <title>Generating Build Profiles with Zend_Dojo_BuildLayer</title>
  239. <para>
  240. One of the chief benefits of a Dojo module layer is that it
  241. facilitates the creation of a custom build.
  242. <classname>Zend_Dojo_BuildLayer</classname> has functionality for
  243. generate build profiles.
  244. </para>
  245. <para>
  246. The simplest use case is to utilize the
  247. <methodname>generateBuildProfile()</methodname> method and send the
  248. output to a file:
  249. </para>
  250. <programlisting language="php"><![CDATA[
  251. $build = new Zend_Dojo_BuildLayer(array(
  252. 'view' => $view,
  253. 'layerName' => 'custom.main',
  254. ));
  255. $profile = $build->generateBuildProfile();
  256. $filename = APPLICATION_PATH . '/../misc/scripts/custom.profile.js';
  257. file_put_contents($filename, $profile);
  258. ]]></programlisting>
  259. <para>
  260. Just like generating layers, you may want to automate this via a
  261. <methodname>dispatchLoopShutdown()</methodname> plugin hook; you
  262. could even simply modify the one shown for generating layers to read
  263. as follows:
  264. </para>
  265. <programlisting language="php"><![CDATA[
  266. class App_Plugin_DojoLayer extends Zend_Controller_Plugin_Abstract
  267. {
  268. public $layerScript = APPLICATION_PATH
  269. . '/../public/js/custom/main.js';
  270. public $buildProfile = APPLICATION_PATH
  271. . '/../misc/scripts/custom.profile.js';
  272. protected $_build;
  273. public function dispatchLoopShutdown()
  274. {
  275. if (!file_exists($this->layerScript)) {
  276. $this->generateDojoLayer();
  277. }
  278. if (!file_exists($this->buildProfile)) {
  279. $this->generateBuildProfile();
  280. }
  281. }
  282. public function generateDojoLayer() { /* ... */ }
  283. public function generateBuildProfile()
  284. {
  285. $profile = $this->getBuild()->generateBuildProfile();
  286. file_put_contents($this->buildProfile, $profile);
  287. }
  288. }
  289. ]]></programlisting>
  290. <para>
  291. As noted, with module layers, you should only create the file once.
  292. </para>
  293. <sect3 id="zend.dojo.build-layers.profiles.options">
  294. <title>Build Profile options</title>
  295. <para>
  296. The above functionality will suffice for most situations. The
  297. only way to customize build profile generation is to provide
  298. additional build profile options to utilize.
  299. </para>
  300. <para>
  301. As an example, you may want to specify what type of
  302. optimizations should be performed, whether or not to optimize
  303. <acronym>CSS</acronym> files in the layer, whether or not to copy tests into the
  304. build, etc. For a listing of available options, you should read
  305. the <ulink url="http://docs.dojocampus.org/build/index">Dojo
  306. Build documentation</ulink> and <ulink
  307. url="http://www.dojotoolkit.org/book/dojo-book-0-9/part-4-meta-dojo/package-system-and-custom-builds">accompanying
  308. documentation</ulink>.
  309. </para>
  310. <para>
  311. Das Setzen dieser Optionen ist trivial: Verwendung der
  312. <methodname>addProfileOption()</methodname>,
  313. <methodname>addProfileOptions()</methodname>, oder
  314. <methodname>setProfileOptions()</methodname> Methoden. Die erste Methode fügt einen
  315. einzelnes Schlüssel und Werte Options Paar hinzu, die zweite fügt mehrere hinzu,
  316. und die dritte überschreibt alle Optionen in der Liste von Schlüssel und Werte
  317. Paaren angegeben sind.
  318. </para>
  319. <para>
  320. Standardmäßig werden die folgenden Optionen gesetzt:
  321. </para>
  322. <programlisting language="javascript"><![CDATA[
  323. {
  324. action: "release",
  325. optimize: "shrinksafe",
  326. layerOptimize: "shrinksafe",
  327. copyTests: false,
  328. loader: "default",
  329. cssOptimize: "comments"
  330. }
  331. ]]></programlisting>
  332. <para>
  333. Man kann jegliche Schlüssel und Werte Paare übergeben; das Dojo Build Skript
  334. ignoriert jene die es nict versteht.
  335. </para>
  336. <para>
  337. Als Beispiel für das Setzen von Optionen:
  338. </para>
  339. <programlisting language="php"><![CDATA[
  340. // Eine einzelne Option:
  341. $build->addProfileOption('version', 'zend-1.3.1');
  342. // Mehrere Optionen:
  343. $build->addProfileOptions(array(
  344. 'loader' => 'xdomain',
  345. 'optimize' => 'packer',
  346. ));
  347. // Oder Optionen überschreiben:
  348. $build->setProfileOptions(array(
  349. 'version' => 'custom-1.3.1',
  350. 'loader' => 'shrinksafe',
  351. 'optimize' => 'shrinksafe',
  352. ));
  353. ]]></programlisting>
  354. </sect3>
  355. </sect2>
  356. </sect1>