如何在外部类中实现RemoteObject?

时间:2011-05-27 17:06:31

标签: flex actionscript-3 remoteobject

下面是代码:

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
}

不幸的是,当我调用loadCurrentSchem()函数时,它没有到达eventHandler。怎么了?

这就是我打电话给班级的方式:

<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:Script>

1 个答案:

答案 0 :(得分:0)

问题是你没有正确确保结果处理程序正确调用。问题是当你使用时:

textfeld.text = schem.info;

您已设置值:

info = info + "Loader called ... \n";

作为文本字段的文本,并且在结果处理程序中不会更改此值。

您至少可以通过两种方式解决此问题:

  • Schem扩展您的EventDispatcher课程,并将事件元数据放在那里。

班级:

[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
}

然后在你的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;

        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>
  • 第二种方法是使用数据绑定。

Schem上课:

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
}

在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}" />

希望这有帮助! :)