我如何在Actionscript类中处理Asynctokens

时间:2011-05-26 10:31:14

标签: flex actionscript-3 remoteobject asynctoken

继承我的代码:

public class Schem
{
    public var info:String="";
    private var ro:RemoteObject = new RemoteObject("Hibernatetest");



    public function Schem()
    {       
        ro.endpoint = "http://jesus/blazeds/messagebroker/amf";
    }

    public function loadCurrentSchem():void
    {


        var token:AsyncToken = ro.getCells();
        token.addResponder(new AsyncResponder(onResult,onFault));

        info = info + "Loader Called ...";


    }

    private function onResult(event:ResultEvent,token:Object):void {
        var cellList:ArrayCollection = event.result as ArrayCollection;
        info = info + "Resulthandler Called";

    }

    private function onFault(event:FaultEvent,token:Object):void
    {

    }
    //Eventhandlers


    //Getters, Setters


}

通过检查我发现的信息字符串,当我调用loadCurrentSchem()时,该类没有到达Resulthanlder。那是为什么?

1 个答案:

答案 0 :(得分:0)

首先,我看不到你在哪里有异步令牌的优势?异步令牌是一种模式,用于在单个对象中封装有关单个查询及其状态的所有信息。您可以阅读更多here

在您的情况下,您需要的只是获取查询结果事件。最好的方法是使用RemoteObject的事件(请参阅documentation)。所以代码如下所示:

public class Schem
{
    public var info:String="";
    private var ro:RemoteObject;



    public function Schem()
    {
        ro = new RemoteObject("Hibernatetest");
        ro.endpoint = "http://jesus/blazeds/messagebroker/amf";
        ro.addEventListener(ResultEvent.RESULT, onResult);
        ro.addEventListener(FaultEvent.FAULT, onFault);
    }

    public function loadCurrentSchem():void
    {
        ro.getCells();
        info = info + "Loader Called ...";
    }

    private function onResult(event:ResultEvent):void {
        var cellList:ArrayCollection = event.result as ArrayCollection;
        info = info + "Resulthandler Called";
    }

    private function onFault(event:FaultEvent):void
    {
        info = info + "Errorhandler Called";
    }
    //Eventhandlers


    //Getters, Setters
}