第四十一讲:tapestry Ajax周期性更新

源码如下:

AJAXPeriodicUpdate.java

/**
 * 项目名称:TapestryStart
 * 开发模式:Maven+Tapestry5.x+Tapestry-hibernate+Mysql
 * 网址: http://www.flywind.org
 * 版本:1.0
 * 编写:飞风
 * 时间:2012-02-29
 */
package com.tapestry.app.pages;
 
import java.util.Date;
 
import org.apache.tapestry5.annotations.Import;
import org.apache.tapestry5.annotations.InjectComponent;
import org.apache.tapestry5.corelib.components.Zone;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.json.JSONObject;
import org.apache.tapestry5.services.Request;
import org.apache.tapestry5.services.javascript.JavaScriptSupport;
 
@Import(library = "context:assets/js/AJAXPeriodicUpdate.js")
public class AJAXPeriodicUpdate {
 
@InjectComponent
private Zone timeZone;
 
@Inject
private JavaScriptSupport javaScriptSupport;
 
@Inject
private Request request;
 
public void setupRender() {
javaScriptSupport.addInitializerCall("periodicTimeZoneUpdater", new JSONObject());
}
 
Object onRefreshTimeZone() {
return request.isXHR() ? timeZone.getBody() : null;
}
 
public Date getServerTime() {
return new Date();
}
}
 

AJAXPeriodicUpdate.tml

<html t:type="layout" title="tapestryStart Index"  t:sidebarTitle="Framework Version" 
xmlns:t="http://tapestry.apache.org/schema/tapestry_5_3.xsd" xmlns:p="tapestry:parameter">
<div style="margin-left: 50px">
<a t:type="eventlink" t:id="refreshTimeZone" href="#" t:zone="timeZone" style="display: none;">Hidden EventLink</a>
 
<t:zone t:id="timeZone">
serverTime:  ${serverTime}
</t:zone><br/>
</div>
</html>

AJAXPeriodicUpdate.js

PeriodicTimeZoneUpdater = Class.create( {
 
initialize: function() {
var eventLink = $('refreshTimeZone');
 
var updatesCount = 0;
 
new PeriodicalExecuter(function(pe) {
if (updatesCount++ < 4) {
Tapestry.findZoneManager(eventLink).updateFromURL(eventLink.href);
}
else {
pe.stop();
}
}, 3);
}
 
})
 
Tapestry.Initializer.periodicTimeZoneUpdater = function(spec) {
new PeriodicTimeZoneUpdater();
}

你可能感兴趣的:(第四十一讲:tapestry Ajax周期性更新)