| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424 |
- <?xml version="1.0" encoding="UTF-8"?>
- <!-- EN-Revision: 16720 -->
- <!-- Reviewed: no -->
- <sect1 id="zend.dojo.build-layers">
- <title>Support für den Build Layer von Zend_Dojo</title>
- <sect2 id="zend.dojo.build-layers.introduction">
- <title>Einführung</title>
- <para>
- Dojo Build Layer bieten einen reinen Pfad von der Entwicklung zur Produktion wenn Dojo
- für den eigenen UI Layer verwendet wird. In der Entwicklung kann man auf-Wunsch laden,
- und schnelles Anwendungs Prototyping erhalten; ein Build Layer nimmt alle Abhängigkeiten
- von Dojo und kompiliert diese in eine einzelne Datei, wobei optional Leerzeichen und
- Kommentare herausgenommen werden, führt Code Heuristiken durch um weitere
- Minimalisierungen von Variablennamen zu erlauben. Zusätzlich kann es auch
- <acronym>CSS</acronym> Minimalisierungen durchführen.
- </para>
- <para>
- Um einen Build Layer zu erstellen würde man traditioneller Weise eine JavaScript Datei
- erstellen die <code>dojo.require</code> Anweisungen für jede Abhängigkeit hat, und
- optional einigen zusätzlichen Code den man ausführen will wen das Skript geladen wird.
- Als Beispiel:
- </para>
- <programlisting language="javascript"><![CDATA[
- dojo.provide("custom.main");
- dojo.require("dijit.layout.TabContainer");
- dojo.require("dijit.layout.ContentPane");
- dojo.require("dijit.form.Form");
- dojo.require("dijit.form.Button");
- dojo.require("dijit.form.TextBox");
- ]]></programlisting>
- <para>
- Auf dieses Skript wird generell als "layer" Skript referiert.
- </para>
- <para>
- Im eigenen Anwendungs Layer, würde man dann Dojo instruieren dieses Modul zu laden:
- </para>
- <programlisting language="html"><![CDATA[
- <html>
- <head>
- <script type="text/javascript" src="/js/dojo/dojo.js"></script>
- <script type="text/javascript">
- dojo.registerModulePath("custom", "../custom/");
- dojo.require("custom.main");
- </script>
- ]]></programlisting>
- <para>
- Wenn man <classname>Zend_Dojo</classname> verwendet um das zu tun, würde man das
- folgende durchführen:
- </para>
- <programlisting language="php"><![CDATA[
- $view->dojo()->registerModulePath('custom', '../custom/')
- ->requireModule('custom.main');
- ]]></programlisting>
- <para>
- Aber da <classname>Zend_Dojo</classname> die verschiedenen <code>dojo.require</code>
- Anweisungen zusammenfügt, wie kann man das eigene Layer Skript erstellen? Man könnte
- jede Seite öffnen, die erstellten <code>dojo.require</code> Anweisungen anschauen und
- Sie herausschneiden und in eine Layer Skript Datei manuell einfügen.
- </para>
- <para>
- Trotzdem existiert eine bessere Lösung: Da <classname>Zend_Dojo</classname> diese
- Informationen bereits zusammenfügt, kann man diese Information einfach herausziehen und
- die eigene Layer Datei erstellen. Das ist der Sinn von
- <classname>Zend_Dojo_BuildLayer</classname>.
- </para>
- </sect2>
- <sect2 id="zend.dojo.build-layers.usage">
- <title>Erstellen eigener Modul Layer mit Zend_Dojo_BuildLayer</title>
- <para>
- Am einfachsten kann man einfach <classname>Zend_Dojo_BuildLayer</classname>
- instanziieren, es dem View Objekt zusammen mit dem Namen des eigenen Modul Layers
- füttern, und es den Inhalt der Layer Datei erstellen lassen; es liegt an einem selbst
- diese anschließend auf die Festplatte zu schreiben.
- </para>
- <para>
- Nehmen wir als Beispiel an das man den Modul Layer "custom.main" erstellen will.
- Angenommen man folgt der vorgeschlagenen Projekt Verzeichnisstruktur, und man
- will JavaScript Dateien unter <filename>public/js/</filename> speichern, dann
- könnte man das folgende tun:
- </para>
- <programlisting language="php"><![CDATA[
- $build = new Zend_Dojo_BuildLayer(array(
- 'view' => $view,
- 'layerName' => 'custom.main',
- ));
- $layerContents = $build->generateLayerScript();
- $filename = APPLICATION_PATH . '/../public/js/custom/main.js';
- if (!dir_exists(dirname($filename))) {
- mkdir(dirname($filename));
- }
- file_put_contents($filename, $layerContents);
- ]]></programlisting>
- <para>
- Wann sollte man das obige durchführen? Damit es korrekt arbeitet, muß man es nach
- der Darstellung aller View Skripte und des Layouts tun um sicherzustellen das der
- <methodname>dojo()</methodname> Helfer vollständig bestückt wurde. Ein einfacher
- Weg um das zu tun ist die Verwendung des Front Controller Plugins, mit einem
- <methodname>dispatchLoopShutdown()</methodname> Hook:
- </para>
- <programlisting language="php"><![CDATA[
- class App_Plugin_DojoLayer extends Zend_Controller_Plugin_Abstract
- {
- public $layerScript = APPLICATION_PATH . '/../public/js/custom/main.js';
- protected $_build;
- public function dispatchLoopShutdown()
- {
- if (!file_exists($this->layerScript)) {
- $this->generateDojoLayer();
- }
- }
- public function getBuild()
- {
- if (null === $this->_build) {
- $this->_build = new Zend_Dojo_BuildLayer(array(
- 'view' => $view,
- 'layerName' => 'custom.main',
- ));
- }
- return $this->_build;
- }
- public function generateDojoLayer()
- {
- $build = $this->getBuild();
- $layerContents = $build->generateLayerScript();
- if (!dir_exists(dirname($this->layerScript))) {
- mkdir(dirname($this->layerScript));
- }
- file_put_contents($this->layerScript, $layerContents);
- }
- }
- ]]></programlisting>
- <note>
- <title>Den Layer nicht in jeder Seite erstellen</title>
- <para>
- Es ist verführerisch das Layer Skript auf jeder einzelnen Seite zu erstellen.
- Aber das ist Ressourcen intensiv da hierbei für jede Seite auf die Festplatte
- geschrieben werden muß. Zusätzlich erhält man keine Vorteile von Client seitigem
- Cachen, da mtime von der Datei sich jedesmal ändert. Die Datei sollte nur
- einmal geschrieben werden.
- </para>
- </note>
- <sect3 id="zend.dojo.build-layers.usage.options">
- <title>BuildLayer options</title>
- <para>
- The above functionality will suffice for most situations. For
- those needing more customization, a variety of options may be
- invoked.
- </para>
- <sect4 id="zend.dojo.build-layers.usage.options.view">
- <title>Setting the view object</title>
- <para>
- While the view object may be passed during instantiation,
- you may also pass it in to an instance via the
- <methodname>setView()</methodname> method:
- </para>
- <programlisting language="php"><![CDATA[
- $build->setView($view);
- ]]></programlisting>
- </sect4>
- <sect4 id="zend.dojo.build-layers.usage.options.layername">
- <title>Setting the layer name</title>
- <para>
- While the layer name may be passed during instantiation,
- you may also pass it in to an instance via the
- <methodname>setLayerName()</methodname> method:
- </para>
- <programlisting language="php"><![CDATA[
- $build->setLayerName("custom.main");
- ]]></programlisting>
- </sect4>
- <sect4 id="zend.dojo.build-layers.usage.options.onload">
- <title>Including onLoad events in the generated layer</title>
- <para>
- <code>dojo.addOnLoad</code> is a useful utility for
- specifying actions that should trigger when the <acronym>DOM</acronym> has
- finished loading. The <methodname>dojo()</methodname> view helper can
- create these statements via its
- <methodname>addOnLoad()</methodname> and
- <methodname>onLoadCapture*()</methodname> methods. In some
- cases, it makes sense to push these into your layer file
- instead of rendering them via your view scripts.
- </para>
- <para>
- By default, these are not rendered; to enable them, pass the
- <property>consumeOnLoad</property> configuration key during
- instantiation:
- </para>
- <programlisting language="php"><![CDATA[
- $build = new Zend_Dojo_BuildLayer(array(
- 'view' => $view,
- 'layerName' => 'custom.main',
- 'consumeOnLoad' => true,
- ));
- ]]></programlisting>
- <para>
- Alternately, you can use the
- <methodname>setConsumeOnLoad()</methodname> method after
- instantiation:
- </para>
- <programlisting language="php"><![CDATA[
- $build->setConsumeOnLoad(true);
- ]]></programlisting>
- </sect4>
- <sect4 id="zend.dojo.build-layers.usage.options.javascript">
- <title>Including captured JavaScript in the generated layer</title>
- <para>
- The <methodname>dojo()</methodname> view helper includes methods for
- capturing arbitrary JavaScript to include in the
- <script> tag containing the various
- <code>dojo.require</code> and <code>dojo.addOnLoad</code>
- statements. This can be useful when creating default data
- stores or globally scoped objects used throughout your
- application.
- </para>
- <para>
- By default, these are not rendered; to enable them, pass the
- <property>consumeJavascript</property> configuration key during
- instantiation:
- </para>
- <programlisting language="php"><![CDATA[
- $build = new Zend_Dojo_BuildLayer(array(
- 'view' => $view,
- 'layerName' => 'custom.main',
- 'consumeJavascript' => true,
- ));
- ]]></programlisting>
- <para>
- Alternately, you can use the
- <methodname>setConsumeJavascript()</methodname> method after
- instantiation:
- </para>
- <programlisting language="php"><![CDATA[
- $build->setConsumeJavascript(true);
- ]]></programlisting>
- </sect4>
- </sect3>
- </sect2>
- <sect2 id="zend.dojo.build-layers.profiles">
- <title>Generating Build Profiles with Zend_Dojo_BuildLayer</title>
- <para>
- One of the chief benefits of a Dojo module layer is that it
- facilitates the creation of a custom build.
- <classname>Zend_Dojo_BuildLayer</classname> has functionality for
- generate build profiles.
- </para>
- <para>
- The simplest use case is to utilize the
- <methodname>generateBuildProfile()</methodname> method and send the
- output to a file:
- </para>
- <programlisting language="php"><![CDATA[
- $build = new Zend_Dojo_BuildLayer(array(
- 'view' => $view,
- 'layerName' => 'custom.main',
- ));
- $profile = $build->generateBuildProfile();
- $filename = APPLICATION_PATH . '/../misc/scripts/custom.profile.js';
- file_put_contents($filename, $profile);
- ]]></programlisting>
- <para>
- Just like generating layers, you may want to automate this via a
- <methodname>dispatchLoopShutdown()</methodname> plugin hook; you
- could even simply modify the one shown for generating layers to read
- as follows:
- </para>
- <programlisting language="php"><![CDATA[
- class App_Plugin_DojoLayer extends Zend_Controller_Plugin_Abstract
- {
- public $layerScript = APPLICATION_PATH
- . '/../public/js/custom/main.js';
- public $buildProfile = APPLICATION_PATH
- . '/../misc/scripts/custom.profile.js';
- protected $_build;
- public function dispatchLoopShutdown()
- {
- if (!file_exists($this->layerScript)) {
- $this->generateDojoLayer();
- }
- if (!file_exists($this->buildProfile)) {
- $this->generateBuildProfile();
- }
- }
- public function generateDojoLayer() { /* ... */ }
- public function generateBuildProfile()
- {
- $profile = $this->getBuild()->generateBuildProfile();
- file_put_contents($this->buildProfile, $profile);
- }
- }
- ]]></programlisting>
- <para>
- As noted, with module layers, you should only create the file once.
- </para>
- <sect3 id="zend.dojo.build-layers.profiles.options">
- <title>Build Profile options</title>
- <para>
- The above functionality will suffice for most situations. The
- only way to customize build profile generation is to provide
- additional build profile options to utilize.
- </para>
- <para>
- As an example, you may want to specify what type of
- optimizations should be performed, whether or not to optimize
- <acronym>CSS</acronym> files in the layer, whether or not to copy tests into the
- build, etc. For a listing of available options, you should read
- the <ulink url="http://docs.dojocampus.org/build/index">Dojo
- Build documentation</ulink> and <ulink
- url="http://www.dojotoolkit.org/book/dojo-book-0-9/part-4-meta-dojo/package-system-and-custom-builds">accompanying
- documentation</ulink>.
- </para>
- <para>
- Setting these options is trivial: use the
- <methodname>addProfileOption()</methodname>,
- <methodname>addProfileOptions()</methodname>, or
- <methodname>setProfileOptions()</methodname> methods. The first
- method adds a single key and value option pair, the second will add
- several, and the third will overwrite any options with the list
- of key and value pairs provided.
- </para>
- <para>
- By default, the following options are set:
- </para>
- <programlisting language="javascript"><![CDATA[
- {
- action: "release",
- optimize: "shrinksafe",
- layerOptimize: "shrinksafe",
- copyTests: false,
- loader: "default",
- cssOptimize: "comments"
- }
- ]]></programlisting>
- <para>
- You can pass in whatever key and value pairs you want; the Dojo
- build script will ignore those it does not understand.
- </para>
- <para>
- As an example of setting options:
- </para>
- <programlisting language="php"><![CDATA[
- // A single option:
- $build->addProfileOption('version', 'zend-1.3.1');
- // Several options:
- $build->addProfileOptions(array(
- 'loader' => 'xdomain',
- 'optimize' => 'packer',
- ));
- // Or overwrite options:
- $build->setProfileOptions(array(
- 'version' => 'custom-1.3.1',
- 'loader' => 'shrinksafe',
- 'optimize' => 'shrinksafe',
- ));
- ]]></programlisting>
- </sect3>
- </sect2>
- </sect1>
|