1

I have Flex/Java project with blazeDS. 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)

Here's the code:

Home.as

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

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. It return simply one object with name and surname (it's just a hello world to understand this damned AsyncCall). The problem is that I don't know how send this result to Home.as with a classic (java) return type. I have the result in onResult method and I don't know how to get it.

I try to follow Brian instructions and I just waste my time. Maybe because I'm not a flex actionscript programmer but I added the code Brian posted and:

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

than in 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

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. 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. At moment I mangage to obtain the resultEvent data in the method defined in the addEventListener call (onPeopleFound in this case). Thanks a lot Brian I think I surely need your help again in future (at least until acceptance of the project). Bye

  • You had a bug in `DataManager` (the private functions were inside the constructor). I assumed that was just a typo when you wrote it here, since you would be getting different error messages, so I fixed it. – IQAndreas Aug 05 '15 at 17:09
  • Thank you but someone can help me withthis issue?? – junior_developer Aug 05 '15 at 19:14

2 Answers2

0

You can adjust method getPerson to have two parameters referencing the callback functions.

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.

Petr Hrehorovsky
  • 1,153
  • 2
  • 14
  • 16
0

One option is to dispatch an event when you get the data back from the Java call:

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

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

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);
    }
}
Community
  • 1
  • 1
Brian
  • 3,850
  • 3
  • 21
  • 37