Archive for March, 2008

Tapestry 5: A Minor AJAX Enhancement

Wednesday, March 19th, 2008

Tapestry 5 has seen a lot of updates over the past couple of months. With the addition of the Autocomplete mixin and the Zone component, AJAX is becoming evermore supported and ingrained. When writing AJAX components, using the Autocomplete source as a reference to “how it should be done” is a great thing to do. It’s concise, simple, and shows that AJAX components are on their way to feeling very natural. AJAX event handlers for components behave much the same as they do for normal ones, with the addition of a few new options in the return type. Tapestry makes it easy to respond with JSON objects, in that you can return a JSONObject:


Object onUpdatelist() {
	JSONObject messages = new JSONObject(
		"{users:[{user: 'clewis', firstName: 'Chris'}]}");
	return messages;
}

Nice. However there is another type that would be a very natural return type, JSONArray (not supported as of r638979). I say this because it’s already available in the API, and developers quite often need to return collections of (JSON) objects. So, why not support JSONArray return values? As it turns out, adding this support is not at all difficult, so today I opened a ticket and provided a patch to do just that. Check it out at: https://issues.apache.org/jira/browse/TAPESTRY-2286. The patch updates tapestry’s configuration module, and requires the additional class. With this addition you can now return JSONArrays, without the bother of wrapping your list in a useless object:


Object onUpdatelist() {
	JSONArray messages = new JSONArray("[{user: 'clewis', firstName: 'Chris'}]");
	return messages;
}

Of course even without compiling this into tapestry, you can provide this class and wire it up through your own module.