Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

That onUpdateTime method is just an ordinary Tapestry event handler, except that it uses an injected AjaxResponseRenderer to tell Tapestry what zone to update when the link is clicked.

Since Tapestry 5.4.2, you can also easily invoke server-side event handlers using the @PublishEvents annotation and the t5/core/ajax JavaScript function, as explained in the 

Zones

Zones are Tapestry's approach to performing partial page updates. A Zone component renders as an HTML element, typically a <div>, and serves as a marker for where dynamically-updated content should be replaced. A zone is recognizable in the DOM because it will have the attribute data-container-type=zone. The client-side support for Zones is keyed off of this attribute and value.

...

You can return an object array, a list, even a single object. You may return objects instead of strings ... and toString() will be used to convert them into client-side strings.

 

Invoking server-side event handler methods from JavaScript

Tapestry 5.4.2 introduced has an API which makes it easy for server-side events to be invoked from JavaScript. In the server-side, you first need to annotate the event handler methods you want exposed with the new @PublishEvent annotation. Then, in JavaScript, all you need to do is to call the existing t5/core/ajax function with the server-side event name/type in the url parameter and with an options parameter containing an element property, be it null or specifying an DOM element to be used as the starting point for finding the event information. Here's one example:

Code Block
languagejava
public class PublishEventDemoComponent
{
    @OnEvent("answer")
    @PublishEvent
    JSONObject answer() {
        return new JSONObject("origin", "componentAction");
    }
    
    @PublishEvent
    JSONObject onAction()
    {
        return new JSONObject("origin", "componentAnswer");
    }   
}

Notice that answer() and onAction() are ordinary event handlers, with nothing specific besides the @PublishEvent annotation.

Code Block
languagexml
<div id="component" xmlns:t="http://tapestry.apache.org/schema/tapestry_5_0_0.xsd">
	<p id="componentParagraph">I'm a <strong id="strong">component</strong></p>
</div>