I have a main mxml file and a phpconnect.as file. When I call the phpconnect-function from the mxml-file the connect works fine also trace in the console works fine. But what's the best way to get the results of the phpconnect.as file back to the mxml-file.
Here's the code main mxml Test02HomeView.mxml:
<?xml version="1.0" encoding="utf-8"?>
<s:View xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark" title="HomeView">
<fx:Declarations>
<!-- Platzieren Sie nichtvisuelle Elemente (z. B. Dienste, Wertobjekte) hier -->
</fx:Declarations>
<fx:Script>
<![CDATA[
import comps.PHPConnect;
protected function phpConnect_btn_clickHandler(event:MouseEvent):void
{
// TODO Auto-generated method stub
var phpconnect:PHPConnect = new PHPConnect();
phpconnect.phpconnect();
}
]]>
</fx:Script>
<s:TextArea id="log_txt" x="10" y="128" height="104"/>
<s:Button id="phpConnect_btn" x="180" y="256" width="130" label="PHPConnect" click="phpConnect_btn_clickHandler(event)"/>
</s:View>
Here's the code from the phpconnect.as
package comps
{
import flash.events.Event;
import flash.events.HTTPStatusEvent;
import flash.events.IEventDispatcher;
import flash.events.IOErrorEvent;
import flash.events.ProgressEvent;
import flash.events.SecurityErrorEvent;
import flash.net.URLLoader;
import flash.net.URLRequest;
public class PHPConnect
{
public function PHPConnect()
{
}
public function phpconnect():void
{
// TODO Auto-generated method stub
var loader:URLLoader = new URLLoader();
configureListeners(loader);
var request:URLRequest = new URLRequest("http://localhost/file.php");
try {
loader.load(request);
} catch (error:Error) {
trace("Unable to load requested data.");
}
}
private function configureListeners(dispatcher:IEventDispatcher):void
{
dispatcher.addEventListener(Event.COMPLETE, completeHandler);
dispatcher.addEventListener(Event.OPEN, openHandler);
dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler);
dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
}
private function completeHandler(event:Event):void {
var loader:URLLoader = URLLoader(event.target);
trace("completeHandler: " + loader.data);
// log_txt.text = loader.data;
}
private function openHandler(event:Event):void {
trace("openHandler: " + event);
}
private function progressHandler(event:ProgressEvent):void {
trace("progressHandler loaded:" + event.bytesLoaded + " total: " + event.bytesTotal);
}
private function securityErrorHandler(event:SecurityErrorEvent):void {
trace("securityErrorHandler: " + event);
}
private function httpStatusHandler(event:HTTPStatusEvent):void {
trace("httpStatusHandler: " + event);
}
private function ioErrorHandler(event:IOErrorEvent):void {
trace("ioErrorHandler: " + event);
}
}
}
Think for someone who uses actionscript and FlashBuilder, this could be a simple question. For me solving this would be great help.
Create a custom event class following the instructions here and dispatch it when your PHPConnect
class has successfully loaded the data which you can pass as a property of the event. You can now subscribe to that event in your main mxml class and access the loaded data via the event object.
Alternatively / additionally you could set the data as the value of a public property of the PHPConnect
class and access it via the instance instead of via the event object.
Test02HomeView.mxml:
protected function phpConnect_btn_clickHandler(event:MouseEvent):void
{
// TODO Auto-generated method stub
var phpconnect:PHPConnect = new PHPConnect();
phpconnect.addEventListener(CustomEvent.LOADED, this.dataLoadedHandler);
phpconnect.phpconnect();
}
private function dataLoadedHandler(event:CustomEvent):void
{
trace("Data loaded:", event.data);
// Or, assuming instance of phpconnect is in scope and has public data property
//trace("Data loaded:", phpconnect.data);
}
PhpConnect.as:
private function completeHandler(event:Event):void {
//var loader:URLLoader = URLLoader(event.target);
trace("completeHandler: " + data);
// log_txt.text = loader.data;
// Don't need to create a new instance of loader, just cast the target property
// Now dispatch a custom event with the data as the payload
dispatchEvent(new CustomEvent(CustomEvent.LOADED, URLLoader(event.target).data));
}