Refreshing a Tapestry zone on a regular basis
Asked Answered
J

2

5

What is the best way to refresh a Tapestry zone on a regular basis to pull changes of a dataset from a server?

Joesphjoete answered 22/6, 2009 at 8:59 Comment(1)
BTW, Similar feature is planned - see issues.apache.org/jira/browse/TAP5-746Tepee
D
4

You could use Prototype's PeriodicalExecuter, and have that call Tapestry's ZoneManager to update the zone:

new PeriodicalExecuter(function(pe) {
    var zoneObject = Tapestry.findZoneManager(element);
    zoneObject.updateFromUrl(updateUrl);
}, 5);
Dissimilation answered 24/6, 2009 at 9:8 Comment(1)
typeo "PeriodicalExecutor" -> "PeriodicalExecuter"Jolin
J
4

Firstly, you'll need to expose the url for your event handler:

public String getModeChangedUrl()
{
    // will call the onModeChanged method
    return resources.createEventLink("ModeChanged").toAbsoluteURI();
}

Then, in a javascript block in your tml assign the url to a variable:

var modeChangedUrl = "${modeChangedUrl}";

Then you need to get a handle to a ZoneManager javascript object:

var zm = Tapestry.findZoneManagerForZone(zoneId);

It's not important which zone you get the ZoneManager for, all this does is facilitate the ajax callback. If the event listener returns a MultiZoneUpdate or an update for a different zone, it will be handled correctly.

I use a dummy zone for marshalling and always return a MultiZoneUpdate even if I'm only updating one zone. Since more often than not I need to update multiple zones, I find it easier to be consistent in my approach. anyway, that is a little off topic for your question.

if you have additional parameters for the event handler, you can append them to the url separated by '/' ie "http://www.host.com/app/page/event/param1/param2"

now that you have the url and a ZoneManager, you can initialise the request-response loop:

zm.updateFromURL(url);

as henning suggested, combining this with the PeriodicalExecuter in prototype will achieve what you want:

new PeriodicalExecuter(function(pe)
    {
        var zm = Tapestry.findZoneManagerForZone("anyZoneId");
        zm.updateFromUrl(url);
    }, 5);
Jolin answered 16/11, 2010 at 22:2 Comment(1)
this has changed somewhat since T5.2. MultiZoneUpdate is a thing of the past. the client side stuff is largely the same though.Jolin

© 2022 - 2024 — McMap. All rights reserved.