What is the best way to refresh a Tapestry zone on a regular basis to pull changes of a dataset from a server?
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);
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);
© 2022 - 2024 — McMap. All rights reserved.