如何在外部类中实现 RemoteObject?

发布于 2024-11-10 00:05:33 字数 1535 浏览 2 评论 0原文

代码如下:

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>

那里。

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

There.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

戏舞 2024-11-17 00:05:33

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

textfeld.text = schem.info;

您已将:的值设置

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

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

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

  • EventDispatcher 扩展您的 Schem 类,并将事件元数据放置在其中。

类:

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

希望这有帮助! :)

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 from EventDispatcher 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! :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文