Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Wordsmithing

...

Tapestry 5.4.2 introduced an API which makes it easy for server-side events to be invoked from JavaScript. On the server-side, you first need to annotate the event handler methods you want to expose with the @PublishEvent annotation. Then, in JavaScript, all you need to do is to call the existing t5/core/ajax function, but with slightly different parameters.

The t5/core/ajax function has two parameters: url and options. The . Prior to Tapestry 5.4.2, the first one was the difficult part to get when doing AJAX requests to event handler methods in Tapestry. You needed to inject ComponentResources in your component class, call componentResources.createEventLink() for each event handler method, then pass all this information back to JS the browser through one of the JavaScriptSupport methods. Since For Tapestry 5.4.2 and later, your JavaScript code only needs to know the event name (also called event type) and optionally indicate a DOM element to be used as a starting point for finding the event URL.

All event data is stored in data-componenent-events attributes. For page classes, it's put in the attribute is added to the <body> element. For components, it's put in added to the first element rendered created by rendering the component. Given an HTML element, the search is done until one in this performed in the following order until information for the given event is first found:

  1. The element itself
  2. The element's previous siblings, closest first (bottom-up)
  3. The element's parents.
  4. The page's <body> element.

 

Here's one example:

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

...