DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Coding
  3. Languages
  4. Tapestry 5.3+ New Features : Part 2

Tapestry 5.3+ New Features : Part 2

Taha Siddiqi user avatar by
Taha Siddiqi
·
Oct. 04, 11 · Interview
Like (0)
Save
Tweet
Share
5.63K Views

Join the DZone community and get the full member experience.

Join For Free

AjaxResponseRender

This is one of the most useful feature of Tapestry 5.3. There is already a concept of Zone in Tapestry for Ajax but now it is complimented by AjaxResponseRenderer. This fills a lot of gaps at least for people coming from Wicket. It is a bit similar to AjaxRequestTarget but empowered with Zones and JSON. I have compiled a small example demonstrating most of the features.

Please Note : This is only a demonstration of how AjaxResponseRenderer is used. Obviously you should always prefer the ‘Zone way’ of doing things. Only when you need a combination of zone updates(multiple), small JavaScript callbacks and JSON callbacks, you can use this service. Also when you want to do different things(explained below) in different event handlers (may be belonging to different components), this service can be of great help.

 

@Import(library = "testJSON.js")
public class NewAjax {
    @Inject
    private AjaxResponseRenderer ajaxResponseRenderer;

    @InjectComponent
    private Zone topZone;

    @InjectComponent
    private Zone bottomZone;

    @Inject
    private JavaScriptSupport javaScriptSupport;

    @InjectComponent
    private EventLink jsonCallbackLink;

    @Inject
    private Messages messages;

    @AfterRender
    void addJavaScript(){
        javaScriptSupport.addInitializerCall("testJSON", jsonCallbackLink.getClientId());
    }

    @OnEvent("serverAlert")
    void showAlert() {
        ajaxResponseRenderer.addCallback(new JavaScriptCallback() {
            public void run(JavaScriptSupport javascriptSupport) {
                javascriptSupport.addScript(
                    String.format("alert('%s');", messages.get("server.hello")));
            }
        });
    }

    @OnEvent("sendJSON")
    void sendJSON() {
        ajaxResponseRenderer.addCallback(new JSONCallback() {
            public void run(JSONObject reply) {
                reply.put("message", messages.get("server.message"));
            }
        });
    }

    @OnEvent("multipleZoneUpdate")
    void showZones() {
        ajaxResponseRenderer.addRender("topZone", topZone).
            addRender("bottomZone", bottomZone);
    }

    public Date getDate() {
        return new Date();
    }

}
<html xmlns:t='http://tapestry.apache.org/schema/tapestry_5_1_0.xsd'>
    <head>
        <title>New Ajax Features</title>
    </head>

    <body>
        <div t:type='zone' t:id='topZone'>
             Zone ${date}
        </div>

        <a href='#' t:type='eventLink'
           t:event='multipleZoneUpdate' t:zone='topZone' t:id='multipleZoneLink'>
            Multiple Zone Update
        </a>

        <div t:type='zone' t:id='bottomZone'>
            Bottom Zone ${date}
        </div>

        <br/>

        <a t:type='eventLink' t:zone='topZone'
           t:id='javaScriptCallbackLink' t:event='serverAlert'>
            Show Feedback alert
        </a>

        <br/>

        <a t:type='eventLink' t:id='jsonCallbackLink' t:event='sendJSON'>
            Get alert message from server!
        </a>
    </body>

</html>
Tapestry.Initializer.testJSON = function(elementId){
    $(elementId).observe("click", function(event){

        Tapestry.ajaxRequest($(elementId).href, function(response){
            alert(response.responseJSON.message);
        });

        event.preventDefault();
    });
};
server.hello=Hello from Server!
server.message=Message from Server!!

For Multiple Zone Update

Instead of using MultiZoneUpdate, you can use AjaxRequestRenderer. The advantage is that it is now a service. You can call it from multiple event handlers present in different components handling the same event. Or imagine an event handler in a component which triggers another event that is handled by the container component. Now the component can add its internal zone and then trigger an event to which the container can respond by adding its own zone!!.

Using JavaScript callback

You can now do something on server-side and return an alert message !!

Using JSON callback

You can send a JSON response in addition to updating multiple zones !!

From http://tawus.wordpress.com/2011/10/01/tapestry-5-3-new-features-part-2/

Event JSON Container JavaScript Concept (generic programming) Advantage (cryptography)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Rust vs Go: Which Is Better?
  • Spring Cloud
  • Journey to Event Driven, Part 1: Why Event-First Programming Changes Everything
  • What Is API-First?

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: