ZendX_JQuery View HelpersZend Framework provides jQuery related View Helpers through its Extras Library. These can be enabled in two ways, adding jQuery to the view helper path:
Or using the ZendX_JQuery::enableView(Zend_View_Interface $view) method that does the same for you. jQuery() View HelperThe 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:
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. Example #1 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:
If you need them throughout your application, you can register them in your bootstrap file using access to the Controller Plugin ViewRenderer:
Now in the view script we want to display a Date Picker and an Ajax based Link.
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: Although jQuery NoConflict ModejQuery offers a noConflict mode that allows the library to be run side by side with other javascript libraries
that operate in the global namespace, Prototype for example. The Zend Framework jQuery View Helper makes usage of
the noConflict mode very easy. If you want to run Prototype and jQuery side by side you can call
Example #2 Building your own Helper with No Conflict Mode To make use of the NoConflict Mode in your own jQuery helper, you only have to use the static method
ZendX_JQuery_View_Helper_JQuery::getJQueryHandler() method. It returns the variable jQuery is operating
in at the moment, either
jQuery UI ThemesSince there are no online available themes to use out of the box, the implementation of the UI library themes
is a bit more complex than with the Dojo helper. The jQuery UI documentation describes for each component
what stylesheet information is needed and the Default and Flora Themes from the downloadable archive give hints
on the usage of stylesheets. The jQuery helper offers the function Methods AvailableThe jQuery() view helper always returns an instance of the jQuery placeholder container. That container object has the following methods available: jQuery Core Library methods
jQuery UI Library methods
jQuery Helper Utility methods
These are quite a number of methods, but many of them are used for internally by all the additional view helpers and during the printing of the jQuery environment. Unless you want to build your own jQuery helper or have a complex use-case, you will probably only get in contact with a few methods of these. Refactoring jQuery environment with setRenderMode()Using the current setup that was described, each page of your website would show a different subset of jQuery code that would be needed to keep the current jQuery related items running. Also different files or stylesheets may be included depending on which helpers you implemented in your application. In production stage you might want to centralize all the javascript your application generated into a single file, or disable stylesheet rendering because you have merged all the stylesheets into a single file and include it statically in your layout. To allow a smooth refactoring you can enable or disable the rendering of certain jQuery environment blocks with help of the following constants and the jQuery()->setRenderMode($bitmask) function.
For an example, if you would have merged jQuery Core and UI libraries as well as other files into a single large file as well as merged stylesheets to keep HTTP requests low on your production application. You could disallow the jQuery helper to render those parts, but render all the other stuff with the following statement in your view:
This statement makes sure only the required javascript statements and onLoad blocks of the current page are rendered by the jQuery helper. MigrationsPrior to 1.8 the methods setCdnVersion(), setLocalPath() setUiCdnVersion() and setUiLocalPath() all enabled the view helper upon calling, which is considered a bug from the following perspective: If you want to use the any non-default library option, you would have to manually disable the jQuery helper aftwards if you only require it to be loaded in some scenarios. With version 1.8 the jQuery helper does only enable itsself, when enable() is called, which all internal jQuery View helpers do upon being called. JQuery HelpersAjaxLink HelperThe AjaxLink helper uses jQuery's ajax capabilities to offer the creation of links that do ajax requests and inject the response into a chosen DOM element. It also offers the possibility to append simple jQuery effects to both the link and the response DOM element. A simple example introduces its functionality: This example creates a link with the label "Link Name" that fires an ajax request to url.php
upon click and renders the response into the div container "#container". The function header
for the ajaxLink is as follows: Available options are:
To enlighten the usage of this helper it is best to show another bunch of more complex examples. This example assumes that you have only one view object that you want to display and don't care a lot about html best practices, since we have to output the jQuery environment just before the closing body tag.
You might have already seen that the 'update', 'complete', and 'beforeSend' options have to be executed in specific order and syntax so that you cannot use those callbacks and override their behaviour completely when you are using ajaxLink(). For larger use cases you will probably want to write the request via jQuery on your own. The primary use case for the callbacks is effect usage, other uses may very well become hard to maintain. As shown in Example Link 5, you can also forward the beforeSend/complete Callbacks to your own javascript functions. Shortcut EffectsYou can use shortcut effect names to make your links actions more fancy. For example the Container that will contain the ajax response may very well be invisible in the first place. Additionally you can use shortcut effects on the link to hide it after clicking. The following effects can be used for callbacks:
jQuery UI Library HelpersThe jQuery UI Library offers a range of layout and form specific widgets that are integrated into the Zend Framework via View Helpers. The form-elements are easy to handle and will be described first, whereas the layout specific widgets are a bit more complex to use. jQuery UI Form HelpersThe method signature for all form view helpers closely resembles the Dojo View helpers signature, helper($id, $value, $params, $attribs). A description of the parameters follows:
The following UI widgets are available as form view helpers. Make sure you use the correct version of jQuery UI library to be able to use them. The Google CDN always offers you the latest released version.
Example #3 Showing jQuery Form View Helper Usage In this example we want to simulate a fictional web application that offers auctions on travel locations. A user may specify a city to travel, a start and end date, and a maximum amount of money he is willing to pay. Therefore we need an autoComplete field for all the currently known travel locations, a date picker for start and end dates and a spinner to specify the amount.
You can see the use of jQuery UI Widget specific parameters. These all correspond to those given in the jQuery UI docs and are converted to JSON and handed through to the view script. Using an Action Helper to Send Data to AutoCompleteThe jQuery UI Autocomplete Widget can load data from a remote location rather than from an javascript array, making its usage really useful. Zend Framework currently providers a bunch of server-side AutoComplete Helpers and there is one for jQuery too. You register the helper to the controller helper broker and it takes care of disabling layouts and renders an array of data correctly to be read by the AutoComplete field. To use the Action Helper you have to put this rather long statement into your bootstrap or Controller initialization function:
You can then directly call the helper to render AutoComplete Output in your Controller
jQuery UI Layout HelpersThere is a wide range of Layout helpers that the UI library offers. The ones covered by Zend Framework view helpers are Accordion, Dialog, Tabs. Dialog is the most simple one, whereas Accordion and Tab extend a common abstract class and offer a secondary view helper for pane generation. The following view helpers exist in the jQuery view helpers collection, an example accompanies them to show their usage.
Example #4 Showing the latest news in a Tab Container For this example we assume the developer already wrote the controller and model side of the script and assigned an array of news items to the view script. This array contains at most 5 news elements, so we don't have to care about the tab container getting to many tabs.
|