jQuery() View Helper
The jQuery() view helper simplifies setup of your jQuery environment
in your application. It takes care of loading the core and ui library dependencies if necessary
and acts as a stack for all the registered onLoad javascript statements. All jQuery view helpers
put their javascript code onto this stack. It acts as a collector for everything jQuery in your
application with the following responsibilities:
Handling deployment of CDN or a local path jQuery Core
and UI libraries.
Handling $(document).ready() events.
Specifying additional stylesheet themes to use.
The jQuery() view helper implementation, like its dojo() pendant,
follows the placeholder architecture implementation; the data set in it persists between view
objects, and may be directly echo'd from your layout script. Since views specified in a Zend_Layout
script file are rendered before the layout itself, the jQuery() helper can act as a
stack for jQuery statements and render them into the head segment of the html page.
Contrary to Dojo, themes cannot be loaded from a CDN for the jQuery UI widgets and have to be implemented
in your pages stylesheet file or loaded from an extra stylesheet file. A default theme called Flora can be
obtained from the jQuery UI downloadable file.
jQuery() View Helper Example
In this example a jQuery environment using the core and UI libraries will be needed. UI Widgets should
be rendered with the Flora thema that is installed in 'public/styles/flora.all.css'. The jQuery libraries
are both loaded from local paths.
To register the jQuery functionality inside the view object, you have to add the appropriate helpers
to the view helper path. There are many ways of accomplishing this, based on the requirements that the
jQuery helpers have. If you need them in one specific view only, you can use the addHelperPath method
on initialization of this view, or right before rendering:
addHelperPath('ZendX/JQuery/View/Helper/', 'ZendX_JQuery_View_Helper');
]]>
If you need them throughout your application, you can register them in your bootstrap
file using access to the Controller Plugin ViewRenderer:
addHelperPath('ZendX/JQuery/View/Helper/', 'ZendX_JQuery_View_Helper');
$viewRenderer = new Zend_Controller_Action_Helper_ViewRenderer();
$viewRenderer->setView($view);
Zend_Controller_Action_HelperBroker::addHelper($viewRenderer);
]]>
Now in the view script we want to display a Date Picker and an Ajax based Link.
ajaxLink("Show me something",
"/hello/world",
array('update' => '#content'));?>
]]>
Both helpers now stacked some javascript statements on the jQuery helper and printed a link and
a form element respectively. To access the javascript we have to utilize the jQuery()
functionality. Both helpers already activated their dependencies that is they have called
jQuery()->enable() and jQuery()->uiEnable(). We only have to print
the jQuery() environment, and we choose to do so in the layout script's head segment:
A jQuery View Helper Example
jQuery(); ?>
layout()->content; ?>