Dojo Form Elements and Decorators | Zend_Dojo Programmer's Reference Guide | Zend_Dom

# Zend_Dojo build layer support

# Introduction

Dojo build layers provide a clean path from development to production when using Dojo for your UI layer. In development, you can have load-on-demand, rapid application prototyping; a build layer takes all Dojo dependencies and compiles them to a single file, optionally stripping whitespace and comments, and performing code heuristics to allow further minification of variable names. Additionally, it can do CSS minification.

In order to create a build layer, you would traditionally create a JavaScript file that has dojo.require statements for each dependency, and optionally some additional code that might run when the script is loaded. As an example:

1. dojo.provide("custom.main");
3. dojo.require("dijit.layout.TabContainer");
4. dojo.require("dijit.layout.ContentPane");
5. dojo.require("dijit.form.Form");
6. dojo.require("dijit.form.Button");
7. dojo.require("dijit.form.TextBox");

This script is generally referred to as a "layer" script.

Then, in your application's layout, you'd instruct Dojo to load this module:

1. <html>
2. <head>
3. <script type="text/javascript" src="/js/dojo/dojo.js"></script>
4. <script type="text/javascript">
5. dojo.registerModulePath("custom", "../custom/");
6. dojo.require("custom.main");
7. </script>

If you use Zend_Dojo to do this, you'd do the following:

1. $view->dojo()->registerModulePath('custom', '../custom/')
2. ->requireModule('custom.main');

But since Zend_Dojo aggregates your various dojo.require statements, how do you create your layer script? You could open each page and view the generated dojo.require statements, and cut and paste them into a layer script file manually.

However, a better solution exists: since Zend_Dojo aggregates this information already, you can simply pull that information and build your layer file. This is the purpose of Zend_Dojo_BuildLayer.

# Generating Custom Module Layers with Zend_Dojo_BuildLayer

At its simplest, you simply instantiate Zend_Dojo_BuildLayer, feed it the view object and the name of your custom module layer, and have it generate the content of the layer file; it is up to you to then write it to disk.

As an example, let's say you wanted to create the module layer "custom.main". Assuming you follow the recommended project directory structure, and that you are storing your JavaScript files under public/js/, you could do the following:

1. $build = new Zend_Dojo_BuildLayer( array (
2. 'view' => $view,
3. 'layerName' => 'custom.main',
4. ));
6. $layerContents = $build->generateLayerScript();
7. $filename = APPLICATION_PATH . '/../public/js/custom/main.js';
8. if (!file_exists ( dirname ($filename))) {
9. mkdir ( dirname ($filename));
10. }
11. file_put_contents($filename, $layerContents);

When should you do the above? For it to work correctly, you need to do it after all view scripts and the layout have been rendered, to ensure that the dojo() helper is fully populated. One easy way to do so is using a front controller plugin, with a dispatchLoopShutdown() hook:

1. class App_Plugin_DojoLayer extends Zend_Controller_Plugin_Abstract
2. {
3. public $layerScript = APPLICATION_PATH . '/../public/js/custom/main.js';
4. protected $_build;
6. public function dispatchLoopShutdown()
7. {
8. if (!file_exists ($this->layerScript)) {
9. $this->generateDojoLayer();
10. }
11. }
13. public function getBuild()
14. {
15. $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper(
16. 'ViewRenderer'
17. );
18. $viewRenderer->initView();
19. if (null === $this->_build) {
20. $this->_build = new Zend_Dojo_BuildLayer( array (
21. 'view' => $viewRenderer->view,
22. 'layerName' => 'custom.main',
23. ));
24. }
25. return $this->_build;
26. }
28. public function generateDojoLayer()
29. {
30. $build = $this->getBuild();
31. $layerContents = $build->generateLayerScript();
32. if (!file_exists ( dirname ($this->layerScript))) {
33. mkdir ( dirname ($this->layerScript));
34. }
35. file_put_contents($this->layerScript, $layerContents);
36. }
37. }

> Note: Do not generate the layer on every page
> It's tempting to generate the layer script on each and every page. However, this is resource intensive, as it must write to the disk on each page. Additionally, since the mtime of the file will keep changing, you will get no benefits of client-side caching. Write the file once.

# BuildLayer options

The above functionality will suffice for most situations. For those needing more customization, a variety of options may be invoked.

# Setting the view object

While the view object may be passed during instantiation, you may also pass it in to an instance via the setView() method:

1. $build->setView($view);

# Setting the layer name

While the layer name may be passed during instantiation, you may also pass it in to an instance via the setLayerName() method:

1. $build->setLayerName("custom.main");

# Including onLoad events in the generated layer

dojo.addOnLoad is a useful utility for specifying actions that should trigger when the DOM has finished loading. The dojo() view helper can create these statements via its addOnLoad() and onLoadCapture() methods. In some cases, it makes sense to push these into your layer file instead of rendering them via your view scripts.

By default, these are not rendered; to enable them, pass the consumeOnLoad configuration key during instantiation:

1. $build = new Zend_Dojo_BuildLayer( array (
2. 'view' => $view,
3. 'layerName' => 'custom.main',
4. 'consumeOnLoad' => true,
5. ));

Alternately, you can use the setConsumeOnLoad() method after instantiation:

1. $build->setConsumeOnLoad(true);

# Including captured JavaScript in the generated layer

The dojo() view helper includes methods for capturing arbitrary JavaScript to include in the <script> tag containing the various dojo.require and dojo.addOnLoad statements. This can be useful when creating default data stores or globally scoped objects used throughout your application.

By default, these are not rendered; to enable them, pass the consumeJavascript configuration key during instantiation:

1. $build = new Zend_Dojo_BuildLayer( array (
2. 'view' => $view,
3. 'layerName' => 'custom.main',
4. 'consumeJavascript' => true,
5. ));

Alternately, you can use the setConsumeJavascript() method after instantiation:

1. $build->setConsumeJavascript(true);

# Generating Build Profiles with Zend_Dojo_BuildLayer

One of the chief benefits of a Dojo module layer is that it facilitates the creation of a custom build. Zend_Dojo_BuildLayer has functionality for generate build profiles.

The simplest use case is to utilize the generateBuildProfile() method and send the output to a file:

1. $build = new Zend_Dojo_BuildLayer( array (
2. 'view' => $view,
3. 'layerName' => 'custom.main',
4. ));
6. $profile = $build->generateBuildProfile();
7. $filename = APPLICATION_PATH . '/../misc/scripts/custom.profile.js';
8. file_put_contents($filename, $profile);

Just like generating layers, you may want to automate this via a dispatchLoopShutdown() plugin hook; you could even simply modify the one shown for generating layers to read as follows:

1. class App_Plugin_DojoLayer extends Zend_Controller_Plugin_Abstract
2. {
3. public $layerScript = APPLICATION_PATH
4. . '/../public/js/custom/main.js';
5. public $buildProfile = APPLICATION_PATH
6. . '/../misc/scripts/custom.profile.js';
7. protected $_build;
9. public function dispatchLoopShutdown()
10. {
11. if (!file_exists ($this->layerScript)) {
12. $this->generateDojoLayer();
13. }
14. if (!file_exists ($this->buildProfile)) {
15. $this->generateBuildProfile();
16. }
17. }
19. public function generateDojoLayer() { /* ... */ }
21. public function generateBuildProfile()
22. {
23. $profile = $this->getBuild()->generateBuildProfile();
24. file_put_contents($this->buildProfile, $profile);
25. }
27. }

As noted, with module layers, you should only create the file once.

# Build Profile options

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.

As an example, you may want to specify what type of optimizations should be performed, whether or not to optimize CSS files in the layer, whether or not to copy tests into the build, etc. For a listing of available options, you should read the » Dojo Build documentation and » accompanying documentation.

Setting these options is trivial: use the addProfileOption(), addProfileOptions(), or setProfileOptions() 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.

By default, the following options are set:

1. {
2. action: "release",
3. optimize: "shrinksafe",
4. layerOptimize: "shrinksafe",
5. copyTests: false,
6. loader: "default",
7. cssOptimize: "comments"
8. }

You can pass in whatever key and value pairs you want; the Dojo build script will ignore those it does not understand.

As an example of setting options:

1. // A single option:
2. $build->addProfileOption('version', 'zend-1.3.1');
4. // Several options:
5. $build->addProfileOptions( array (
6. 'loader' => 'xdomain',
7. 'optimize' => 'packer',
8. ));
10. // Or overwrite options:
11. $build->setProfileOptions( array (
12. 'version' => 'custom-1.3.1',
13. 'loader' => 'shrinksafe',
14. 'optimize' => 'shrinksafe',
15. ));

Dojo Form Elements and Decorators | Zend_Dojo Programmer's Reference Guide | Zend_Dom