If you want your flash component to talk to an event handler in your tapestry page and then process the response. The sequence will be
This worked example is part of the unit tests for this library. To see this working checkout the code and in this project type
mvn jetty:run
Jetty will start and on http://localhost:8080/SwfObjectAjaxPage you will see the working example.
On your page class you need to * add the flash movie
public class SwfObjectAjaxPage {
@Property
private JSONObject flashvars;
@Inject
private ComponentResources componentResources;
@Environmental
private RenderSupport renderSupport;
public void setupRender() {
flashvars = new JSONObject();
flashvars.append("ajaxRequestUrl", componentResources.createEventLink("ajaxRequest").toAbsoluteURI() );
}
/**
* Called on the AJAX Request
*
* Note we could return the json object directly if we got flash to set the XMLHTTPRequest headers.
* @return
*/
public Object onAjaxRequest(){
JSONObject myResults = new JSONObject();
myResults.append("Cat", "Parsnip");
return new TextStreamResponse("application/json", myResults.toString());
}
}
<html xmlns:t="http://tapestry.apache.org/schema/tapestry_5_1_0.xsd">
<head></head>
<body>
<h1>Swf Object Test Page</h1>
<div t:id="swfObject" t:type="ioko/swfObject" width="300" height="300" swf="asset:FlexAjax.swf"
flashvars="flashVars">
<p>Non Flash Content</p>
</div>
</body>
In the movie you need to * Get the url from the flash vars
private function setupAjax():void{
ajaxHelper = new AjaxHelper(Application.application.parameters.ajaxRequestUrl, processResult);
ajaxHelper.ExecuteAjax();
}
public class AjaxHelper
{
private var url:URLRequest
private var dataReciever:Function;
public var lastData:Object;
public function AjaxHelper(passedUrl:String, dataReciever:Function)
{
// Passed URL is relative to current site so we need to add the domain name etc on the front
var pageUrl:String = ExternalInterface.call("window.location.href.toString");
var fixedUrl:String = URLUtil.getProtocol(pageUrl) + "://" + URLUtil.getServerNameWithPort(pageUrl) + passedUrl;
url = new URLRequest(fixedUrl);
this.dataReciever = dataReciever; }
public function ExecuteAjax():void{
var loader:URLLoader = new URLLoader;
loader.addEventListener(Event.COMPLETE, function(e:Event):void {
lastData = JSON.decode(loader.data);
dataReciever(lastData);
})
loader.load(url);
}
}