May 17, 2010 0
Creating a news feed in Tapestry 5
News feeds are frequently encountered in today’s web interfaces. Using Ajax, news and social feeds are continually monitored with new items being retrieved for display. In Apache Tapestry 5, the lingua-franca of performing these types of Ajax updates is the Zone component. The Zone component marks (or contains) a region of your Tapestry template that you wish to replace on an Ajax event; however, with news feeds you typically want to add new markup to a page without replacing the existing markup.
Currently, Tapestry does not behave this way. But with the ease of a custom mixin (a way Tapestry can add or alter the behavior of a component), we can easily change a Zone to insert the new markup. And with the addition of another simple mixin, we can continue to poll for new markup to insert!
This tutorial requires previous knowledge of mixins and ajax in Tapestry, as well as some familiarity with Prototype, a JavaScript framework used by Tapestry.
Inserting markup instead of replacing
First, we will alter the behavior of a zone to insert instead of replace markup. This does not require much code. All you need is to add a mixin to the zone component that alters the baked in Tapestry behavior. In this example, we will call this mixin the ZoneInserter.
/** * Modifies how a the content of a Tapestry Zone is updated. Allows content to * be inserted instead of just updating an element. */ @IncludeJavaScriptLibrary("ZoneInserter.js") public class ZoneInserter { /** * Accepted insertion points are: * <ul> * <li>before (as element's previous sibling)</li> * <li>after (as element's next sibling)</li> * <li>top (as element's first child)</li> * <li>bottom (as element's last child) [<b>default</b>]</li> * </ul> */ @Parameter(defaultPrefix = BindingConstants.LITERAL, value = "bottom") private String insertion; @InjectContainer private Zone zone; @Inject private ComponentResources resources; @Inject private RenderSupport renderSupport; void afterRender() { final String id = zone.getClientId(); final JSONObject config = new JSONObject(); if (resources.isBound("insertion")) config.put("insertion", insertion); renderSupport.addInit("zoneInserter", new JSONArray(id, config)); } }
This is a very simple mixin that passes the client id of the zone and a single configuration parameter (“insertion”) to a JavaScript initialization script. The mixin needs to run after the zone renders so that the zones client id is known. Read the rest of this entry »
Recent Comments