AjaxDirectLink
Derivative of DirectLink with support for ajax requests. This component behaves the same as a DirectLink, in that you can directly invoke a listener method on one of your components/pages. It has the addiontal functionality of dealing with ajax logic, and also serves as the main ajax-related starting point for many ajax actions.
The inherited parameters of DirectLink are purposefuly left out, for a list of those parameters consult the documentation for DirectLink.
Many new functions have been added to this component, such as the ability to dynamically update a status area with text during a request. (Like gmail does) Effects parameters have also been added allowing you to specify dynamic html effects to be applied to all or specific components in the request, without having to write any additional javascript.
See also: DirectLink , Live Demo
Parameters
Name | Type | Direction | Required | Default | Description |
---|---|---|---|---|---|
updateComponents | List (or comma-delimited String) | in | no | List of component id strings to refresh after invoking listener. This causes the contents of any html element on the page with a matching id in this list to be replaced, if the specified element is rendered in the return response. Each value must correspond to the value of the id attribute of any html element instrumented as a tapestry component. Tacos will refresh the body of each element after the request. | |
effects | String | in | no | Specifies a list of effects to be applied to the returned response. Read the Effects-Popups page of the UserGuide for more details. | |
preEffects | String | in | no | The same with effects parameter, but specifies effects that should be performed on parts before triggering the ajax request. Read the Effects-Popups page of the UserGuide for more details. | |
popup | String | in | no |
If specified, the updated content will be shown in a floating pane. Read the Effects-Popups page of the UserGuide for more details. |
|
listener | IActionListener | in | no | An optional listener (typically specified as the name of a listener method), notified when the component is triggered. | |
action | IActionListener | in | no | A listener that is notified if this component is triggered just before the form's listener, after all components enclosed by the Form have had a chance to update their properties. | |
updateObject | String | in | no | If specified, the string passed in will be used to build a javascript statement which will call updateObject.ajaxUpdate(element, responseElement, elementId) / updateObject.beforeAjaxUpdate(responseElements) / updateObject.responseComplete(responseElements) | |
statusElement | String | in | no | If specified, the html element on your page with a matching id to this parameter will be temporarily displayed for a few seconds with status text set on the AjaxWebRequest via addStatusResponse(String). | |
direct | boolean | in | no | false | Specifies whether or not components should be invoked directly. This will cause the engine service to only call renderComponent on the targeted components of this request, whereas normal behaviour is to render the whole page and only return the portions of the response that were requested. Use at your own risk, components contained by lists, like Foreach and For will likely not be directly updateable. |
parameters | Collection | in | no | Parameter(s) gathered at the time the button is triggered, supplied as listener parameters in the IRequestCycle available to the listener. If the parameter is a Collection, it will be converted to an Object array (to match the IRequestCycle getListenerParameters() signature). Allows listeners provided by the 'action' parameter to access any rewinded state not conveniently placed using tag/selected (e.g. when there are multiple objects to select as might happen with a nested For). | |
stateful | boolean | in | no | ||
disabled | boolean | in | no | ||
id | String | in | no | id | |
anchor | String | in | no | ||
renderer | ILinkRenderer | in | no |
Body: rendered
Informal parameters: allowed
Reserved parameters: href, onclick
Examples
In this example the AjaxDirectLink component is used to partially refresh one component in a list.
HTML template
<div jwcid="selectedCountries@Any" id="selectedCountries" > <span jwcid="@If" condition="ognl:not selectedItems.empty"> <h3>Selected Countries</h3> <ul id="countryDrop"> <li jwcid="@tacos:PartialFor" source="ognl:selectedItems" value="ognl:selectItem" element="li"> <span jwcid="@Insert" value="ognl:selectItem"/> </li> </ul> </span> </div> <div id="status"></div> <div id="countries"> <span jwcid="foreachItem" > <div jwcid="@Any" class="ognl:beans.evenOdd.next" id="ognl:currItem.toString()" > <div> <a jwcid="linkToggle" class="toggle"> <span jwcid="@Insert" value="ognl:currSelected ? 'Hide' : 'Details'"/> </a> <span jwcid="@Insert" value="ognl:currItem.displayCountry" class="ognl:currSelected ? 'detailHeading' : 'heading'"/> </div> <span jwcid="@If" condition="ognl:currSelected"> <div class="countryDetail"> <div class="flag"> <img jwcid="flagImage" width="34" height="18"/><br/> <span jwcid="@Insert" value="ognl:currItem.country"/> </div> <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. In consequat lectus a eros. Vestibulum tortor. Cras sapien. Morbi sed ipsum. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos hymenaeos. Fusce aliquam auctor tortor.</p> <p style="color: #666">Rendered on: <span jwcid="insertTime"/>.</p> </div> </span> </div> </span> </div> <div id="partialWait"> <img src="img/wait.gif" align="bottom" width="16" height="16"/>Processing... </div>
Page specification
<inject property="ajaxRequest" object="service:tacos.AjaxWebRequest" /> <property name="currItem" /> <property name="selectItem" /> <property name="selectedItems" persist="session" initial-value="ognl:new java.util.TreeSet()"/> <component id="foreachItem" type="tacos:PartialFor"> <binding name="source" value="items"/> <binding name="value" value="currItem"/> <binding name="evenOdd" value="ognl:page.beans.evenOdd" /> </component> <component id="linkToggle" type="tacos:AjaxDirectLink"> <binding name="listener" value="listener:toggleItem"/> <binding name="parameters" value="ognl:currItem.toString()"/> <binding name="updateComponents" value="ognl:{'selectedCountries', currItem.toString()}"/> <binding name="effects" value="template:{highlight:{selectedCountries:'[255,255,184], 500, 500','${currItem}':'[255,255,184], 500, 500'}}"/> <binding name="statusElement" value="literal:status" /> </component> <component id="flagImage" type="Any"> <binding name="src" value="ognl:'http://setiathome.free.fr/images/flags/' + currItem.country.toLowerCase() + '.gif'"/> </component> <component id="insertTime" type="Insert"> <binding name="format" value="@net.sf.tacos.demo.pages.ajax.EffectsExample@DATE_FORMAT"/> <binding name="value" value="new java.util.Date()"/> </component> <bean name="evenOdd" class="org.apache.tapestry.bean.EvenOdd"/>
Java sources
public abstract class PartialPrototype extends BasePage { public static final DateFormat DATE_FORMAT = DateFormat .getDateTimeInstance(DateFormat.SHORT, DateFormat.LONG); /** * Gets the items * @return */ public List getItems() { Set countries = new HashSet(); Locale[] locales = Locale.getAvailableLocales(); List items = new ArrayList(locales.length); for(int i = 0; i < locales.length; i++) { Locale l = locales[i]; if (!"".equals(l.getDisplayCountry()) && !countries.contains(l.getCountry())) { items.add(l); countries.add(l.getCountry()); } } Collections.sort(items, new Comparator() { public int compare(Object o1, Object o2) { Locale l1 = (Locale)o1; Locale l2 = (Locale)o2; return l1.getDisplayCountry().compareTo(l2.getCountry()); } }); return items; } /** * Toggles an item being selected * @param cycle */ public void toggleItem(IRequestCycle cycle) { String item = (String)cycle.getListenerParameters()[0]; Set s = getSelectedItems(); if (s.contains(item)) { s.remove(item); } else { s.add(item); } setSelectedItems(s); AjaxWebRequest ajax = (AjaxWebRequest)cycle .getAttribute(AjaxWebRequest.AJAX_REQUEST); if (ajax != null) ajax.addStatusResponse("File selected.."); } /** * If current item is selected * @return */ public boolean isCurrSelected() { return getSelectedItems().contains(getCurrItem().toString()); } /** get current item */ public abstract Locale getCurrItem(); /** gets selected items */ public abstract Set getSelectedItems(); /** sets selected items */ public abstract void setSelectedItems(Set selectedItems); }