Cimlman
Cimlman

Reputation: 3814

Partial markup via Tapestry block

I'm using Tapestry 5.3.

In a Tapestry component, I'm trying to implement an event handler that creates responses to AJAX requests.

I know that I can call MarkupWriterFactory#newPartialMarkupWriter to get an instance of MarkupWriter and then generate the response using this instance. The built-in mixin Autocomplete generates responses to AJAX requests in this way. Here is a trivial example:

@Inject
private MarkupWriterFactory markupWriterFactory;

@OnEvent("myevent")
Object generatePartialMarkup() {
    final ContentType contentType = new ContentType("text/html");
    final MarkupWriter markupWriter = markupWriterFactory.newPartialMarkupWriter(contentType);
    markupWriter.element("hr");
    markupWriter.end();
    return new TextStreamResponse(contentType.toString(), markupWriter.toString());
}

However, this is a clumsy solution for a partial HTML that is much more complex than just a hr element. I wonder if there is a chance to create such response using a Tapestry block rather than generating the response "manually" via MarkupWriter. If I just return an instance of block from the event handler, an exception is thrown when the event is triggered.

Thanks.

Upvotes: 0

Views: 191

Answers (1)

lance-java
lance-java

Reputation: 27994

Looks like you're making life difficult for yourself. From what I can see you can solve this with an eventlink, a zone and an event handler which returns a Block. See here for more info.

Not entirely sure what you're doing but if you need to render a block as a string, see the capture component here

Upvotes: 1

Related Questions