简体   繁体   English

Flex操作脚本,从AsyncToken调用获取数据

[英]Flex actionscript, get data from AsyncToken call

I have Flex/Java project with blazeDS. 我有blazeDS的Flex / Java项目。 Now I have an actionscript file that call a method of another actionscript that call the remoteObject (java class who make a simple select on db) 现在,我有一个动作脚本文件,该文件调用另一个动作脚本的方法,该方法调用remoteObject(在db上进行简单选择的java类)

Here's the code: 这是代码:

Home.as Home.as

..
private var _dm:DataManager = new DataManager;
public function getPerson():void { // this is connect to a button in .mxml
    _dm.getPerson();
}
..

DataManager.as DataManager.as

public class DataManager {
    private var _service:RemoteObject;
    private var _url:URLRequest;
    private var loCs:ChannelSet = new ChannelSet();

    public function DataManager () {
        _service = new RemoteObject("PeopleDAO");
        loCs.addChannel(new AMFChannel("canale", "http://localhost:8080/FlexTRYOUT/messagebroker/amf"));
        _service.channelSet = loCs;
    }

    private function onFault(event:FaultEvent,token:Object):void {
        var _fail:String = "fault";
    }
    private function onResult(event:ResultEvent,token:Object):void {
        per = event.result as People; // is a bean class
        Alert.show(per.nome);
    }

    public function getPerson():void {
        var token:AsyncToken = _service.getPersona();
        token.addResponder(new AsyncResponder(onResult,onFault));
    }
}

The call works fine, it calls java method names getPerson() of the DataManger.java class. 该调用工作正常,它调用DataManger.java类的java方法名称getPerson() It return simply one object with name and surname (it's just a hello world to understand this damned AsyncCall). 它仅返回一个带有名称和姓氏的对象(理解这个该死的AsyncCall只是一个问候世界)。 The problem is that I don't know how send this result to Home.as with a classic (java) return type. 问题是我不知道如何使用经典(java)返回类型将此结果发送到Home.as I have the result in onResult method and I don't know how to get it. 我将结果保存在onResult方法中,但我不知道如何获取它。

I try to follow Brian instructions and I just waste my time. 我尝试遵循Brian的指示,只是浪费时间。 Maybe because I'm not a flex actionscript programmer but I added the code Brian posted and: 可能是因为我不是Flex动作脚本程序员,但我添加了Brian发布的代码,并:

public function getPerson():void { // this is connect to a button in .mxml
    _dm.addEventListener(DATA_RECEIVED, onPersonFound); * compile error 1
    _dm.getPerson();
}

error is DATA_RECEIVED is undefined 错误是DATA_RECEIVED未定义

than in DataManager: 比在DataManager中:

public class DataManager {
    public static const DATA_RECEIVED:String = "DATA_RECEIVED";

    ...

    private function onResult(event:ResultEvent,token:Object):void {
        per = event.result as People; // is a bean class
        dispatchEvent(new DataReceivedEvent(DATA_RECEIVED, per)); * compile error 2
    }
}

error 2 is call of possible undefined method dispatchEvent 错误2是可能的未定义方法dispatchEvent的调用

Where is the mistake? 错误在哪里? Please guys write the complete code because I'm on flex - actionscript - blazeds from two days and I have a few time to try solution. 请大家编写完整的代码,因为两天后我就开始使用flex-actionscript-了,而我有一些时间来尝试解决方案。 Thanks 谢谢

OK, Sorry for all this post, I just create new one (but more elaborated and clear) with the same question. 好的,对不起,所有这篇文章,我只是用相同的问题创建一个新的(但更加详尽和清楚)。 Step by Step I'm studing this language and I manage to implement the Brian code but DataManager.as class must extend EventDispatcher, if I don't extend this I have the compile error I posted. 我正在逐步学习这种语言,并设法实现Brian代码,但DataManager.as类必须扩展EventDispatcher,如果不扩展该类,则会遇到我发布的编译错误。 At moment I mangage to obtain the resultEvent data in the method defined in the addEventListener call (onPeopleFound in this case). 此刻,我试图通过addEventListener调用(在这种情况下为onPeopleFound)中定义的方法来获取resultEvent数据。 Thanks a lot Brian I think I surely need your help again in future (at least until acceptance of the project). 非常感谢Brian,我以后肯定会再次需要您的帮助(至少在接受该项目之前)。 Bye 再见

You can adjust method getPerson to have two parameters referencing the callback functions. 您可以将方法getPerson调整为具有两个引用回调函数的参数。

public function getPerson(onResultCallback:Function, onFaultCallback:Function):void {
   var token:AsyncToken = _service.getPersona();
   token.addResponder(new AsyncResponder(onResultCallback,onFaultCallback));
}

This way you can receive data in an instance of the class you need. 这样,您可以在所需类的实例中接收数据。

One option is to dispatch an event when you get the data back from the Java call: 一种选择是当您从Java调用获取数据时调度一个事件:

Home.as Home.as

...
public function getPerson():void { // this is connect to a button in .mxml
    _dm.addEventListener(DATA_RECEIVED, onPersonFound);
    _dm.getPerson();
}

private function onPersonFound(dataEvent:DataReceivedEvent):void {
    var person:People = dataEvent.people;
    //Do important processing...
}
...

In DataManager.as 在DataManager.as中

public class DataManager {
    public static const DATA_RECEIVED:String = "DATA_RECEIVED";

    ...

    private function onResult(event:ResultEvent,token:Object):void {
        per = event.result as People; // is a bean class
        dispatchEvent(new DataReceivedEvent(DATA_RECEIVED, per));
    }
}

And DataReceivedEvent.as will look like the answer to How to dispatch an event with added data - AS3 而且DataReceivedEvent.as看起来像是如何使用添加的数据来调度事件的答案-AS3

public class DataReceivedEvent extends Event
{
    public static const DATA_RECEIVED:String = "DATA_RECEIVED";

    // this is the object you want to pass through your event.
    public var result:Object;

    public function DataReceivedEvent(type:String, result:Object, bubbles:Boolean=false, cancelable:Boolean=false)
    {
        super(type, bubbles, cancelable);
        this.result = result;
    }

    // always create a clone() method for events in case you want to redispatch them.
    public override function clone():Event
    {
        return new DataReceivedEvent(type, result, bubbles, cancelable);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM