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