Em Mon, 14 Dec 2009 23:02:41 -0200, Ashwanth Kumar <ashwanth.ku...@gmail.com> escreveu:

Well, if you want Mouse and Text field (key press events), use Java Script on the client side, its very useful and efficient. If you're very particular, use DWR to map JS events to a Java Class @ server side, but with in-built ajax support, Tapestry doesn't require it though.

I don't see the need for DWR for implementing something that Tapestry doesn't implement out-of-the-box. I have one example, but its written in Portuguese.

Solution outline:

1) Define an event name.

2) In your page, component or mixin class, @Inject ComponentResources and use its createEventLink() to create a link that will trigger that event.

3) Still in the same class, create a method with @OnEvent("yourEventName") that handles the event and returns a JSONObject or JSONArray.

4) @Inject RenderSupport and use its addScript() method to generate any needed initialization for your JavaScript code, including the event URL.

5) If you use Prototype, use Event.observe('elementId', 'eventName', function() { implement your handling here ; }) to listen to the event and invoke the event method usint its URL. You'll probably use Ajax.Request. transport.responseJSON is exactly the JSONObject or JSONArray you returned in your event handler method.

A simple template you can use, based in real code, follows. It reacts to a change in a select tag, posts its value, gets the response as a JSON object and the changes the value of some <span>s with the object properties.

/* This URL is the one created by ComponentResources.createEventLink().
This is the function which is invoked by the JavaScript line added via RenderSupport.addScript().
*/
function initialize(url) {

        Event.observe('selectId', 'change', function() {
                
                new Ajax.Request(url, {
                        method : 'post',
                        parameters: { value: $F('selectId') },
                        onSuccess: function(transport) {
                                var result = transport.responseJSON;
                                $('span1').innerHTML = result.property1;
                                $('span2').innerHTML = result.property2;
                                $('span3').innerHTML = result.property3;
                        }
                });
                
        });
        
}

I hope it helps.

--
Thiago H. de Paula Figueiredo
Independent Java, Apache Tapestry 5 and Hibernate consultant, developer, and instructor Owner, software architect and developer, Ars Machina Tecnologia da Informação Ltda.
http://www.arsmachina.com.br

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tapestry.apache.org
For additional commands, e-mail: users-h...@tapestry.apache.org

Reply via email to