How do i implement RemoteObject in a foreign class?
Heres the code:
public class Schem
{
public var info:String="";
public function Schem()
{
}
public function loadCurrentSchem():void
{
var ro:RemoteObject = new RemoteObject("Hibernatetest");
ro.endpoint = "http://Jesus/blazeds/messagebroker/amf";
ro.addEventListener(ResultEvent.RESULT,onResult);
ro.getCells();
info = info + "Loader called ... \n";
}
public function onResult(event:ResultEvent):void
{
var array:ArrayCollection = event.result as ArrayCollection;
info = info + "Schemlength = " + String(array.length)+ "\n";
}
private function onFault(event:FaultEvent):void
{
info = info + "Errorhandler Called";
}
//Eventhandlers
//Getters, Setters
}
Unfortunatly, its doesnt reach the eventHandler, when i call the loadCurrentSchem() function. Whats wrong?
This is how i call the class:
<fx:Script>
<![CDATA[
import argoseye.main.Golem;
import argoseye.main.Schem;
import mx.collections.ArrayCollection;
import mx.rpc.AsyncToken;
import mx.rpc.Responder;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
import mx.rpc.remoting.RemoteObject;
protected function button1_clickHandler(event:MouseEvent):void
{
var schem:Schem = new Schem();
schem.loadCurrentSchem();
textfeld.text = schem.info;
}
]]>
</fx:Scr开发者_开发百科ipt>
There.
The problem is you haven't proper made sure the result handler called correctly. The thing is that when you use:
textfeld.text = schem.info;
you've set the value of:
info = info + "Loader called ... \n";
as a text of the text field and this value isn't changed in a result handler.
You can resolve this issue at least two ways:
- Extend your
Schem
class fromEventDispatcher
and place events metadata there.
The class:
[Event(name="result", type="mx.rpc.events.ResultEvent")]
[Event(name="fault", type="mx.rpc.events.FaultEvent")]
public class Schem extends EventDispatcher
{
public var info:String="";
public function Schem()
{
}
public function loadCurrentSchem():void
{
var ro:RemoteObject = new RemoteObject("Hibernatetest");
ro.endpoint = "http://Jesus/blazeds/messagebroker/amf";
ro.addEventListener(ResultEvent.RESULT,onResult);
ro.addEventListener(FaultEvent.FAULT,onFault);
ro.getCells();
info += "Loader called ... \n";
}
private function onResult(event:ResultEvent):void
{
var array:ArrayCollection = event.result as ArrayCollection;
info += "Schemlength = " + String(array.length)+ "\n";
dispatchEvent(event);
}
private function onFault(event:FaultEvent):void
{
info += "Errorhandler Called";
dispatchEvent(event);
}
//Eventhandlers
//Getters, Setters
}
Then in your MXML class:
<fx:Script>
<![CDATA[
import argoseye.main.Golem;
import argoseye.main.Schem;
import mx.collections.ArrayCollection;
import mx.rpc.AsyncToken;
import mx.rpc.Responder;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
import mx.rpc.remoting.RemoteObject;
protected function button1_clickHandler(event:MouseEvent):void
{
var schem:Schem = new Schem();
schem.addEventListener(ResultEvent.RESULT,onResult);
schem.addEventListener(FaultEvent.FAULT,onFault);
schem.loadCurrentSchem();
textfeld.text = schem.info;
}
private function onResult(event:ResultEvent):void
{
textfeld.text = event.currentTarget.info;
}
private function onFault(event:FaultEvent):void
{
textfeld.text = event.currentTarget.info;
}
]]>
</fx:Script>
- The second way is to use data binding.
Schem
class:
public class Schem
{
[Bindable]
public var info:String="";
public function Schem()
{
}
public function loadCurrentSchem():void
{
var ro:RemoteObject = new RemoteObject("Hibernatetest");
ro.endpoint = "http://Jesus/blazeds/messagebroker/amf";
ro.addEventListener(ResultEvent.RESULT,onResult);
ro.addEventListener(FaultEvent.FAULT,onFault);
ro.getCells();
info += "Loader called ... \n";
}
private function onResult(event:ResultEvent):void
{
var array:ArrayCollection = event.result as ArrayCollection;
info += "Schemlength = " + String(array.length)+ "\n";
}
private function onFault(event:FaultEvent):void
{
info += "Errorhandler Called";
}
//Eventhandlers
//Getters, Setters
}
And in MXML:
<fx:Script>
<![CDATA[
import argoseye.main.Golem;
import argoseye.main.Schem;
import mx.collections.ArrayCollection;
import mx.rpc.AsyncToken;
import mx.rpc.Responder;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
import mx.rpc.remoting.RemoteObject;
[Bindable]
private var schem:Schem;
protected function button1_clickHandler(event:MouseEvent):void
{
schem = new Schem();
schem.loadCurrentSchem();
}
]]>
</fx:Script>
<s:Label id="textfeld" text="{schem.info}" />
Hope this helps! :)
精彩评论