Returning results from remoteobject class to parent

389 views Asked by At

I have a Flex application using ColdFusion to retrieve data from MS SQL. I'm trying to create a class where I can send in a numeric argument and it returns a value to the document calling the class.

This is my class

package com.procost
{
import mx.controls.Alert;
    import mx.core.FlexGlobals;
    import mx.rpc.AbstractOperation;
    import mx.rpc.events.ResultEvent;
    import mx.rpc.remoting.RemoteObject;

    public class EmailListRetrieve
    {
        public var emailListId:Number = -1;

        public function send():void{
            //Create the remote object
            var _remoteObject:RemoteObject = new RemoteObject('test');
            _remoteObject = new RemoteObject("ColdFusion");
            _remoteObject.endpoint = "http://" + FlexGlobals.topLevelApplication.endPointLink + "/flex2gateway/";
            _remoteObject.source = FlexGlobals.topLevelApplication.remotePath + "services.general";
            _remoteObject.showBusyCursor = true;

            //Send
            var op:AbstractOperation = _remoteObject.getOperation('getEmailList');
            op.addEventListener(ResultEvent.RESULT, result);
            op.send(this);
        }

        // Result from CFC
        private function result(event:ResultEvent){
            Alert.show(event.result.toString());
        }
    }
}

**This is how I'm calling it from my MXML **

import com.procost.EmailListRetrieve;    

public function fncClick():void{
         var request:EmailListRetrieve = new EmailListRetrieve();
         request.emailListId=1;
         request.send();
    }

The result function in my class is returning all the data I need from the DB. The issue is, how do I get this data back into the MXML document I called it from?

Any help would be appreciated.

2

There are 2 answers

0
Brian On

All you need to do is echo the data from the ResultEvent in another event (note that you'll want a custom event class to carry the data):

public class EmailListRetrieve extends EventDispatcher
{
    public static const DATA_READY:String = "dataReady";
    //The rest of the class is unchanged

    private function result(event:ResultEvent){
        Alert.show(event.result.toString());
        dispatchEvent(new MyDataEvent(DATA_READY, event.result.toString()));
    }
}

Then, in your MXML code:

import com.procost.EmailListRetrieve;    

public function fncClick():void{
    var request:EmailListRetrieve = new EmailListRetrieve();
    request.emailListId=1;
    request.send();
    request.addEventListener(EmailListRetrieve.DATA_READY, onListReady);
}

public function onListReady(e:MyDataEvent):void {
    var importantData = e.data;
    //Pass importantData to whatever needs to display it in your MXML
}

Important: Your request EmailListRetrieve object could end up getting garbage collected before you receive a response. I recommend saving a reference to it in a class-level variable in your MXML instead of having it be a function member.

0
Y Borys On

You can use Responder to send data back to main class.

public class EmailListRetrieve
    {
    .....
    public var callback:IResponder;
    .....        
    // Result from CFC
    private function result(event:ResultEvent){
        //Alert.show(event.result.toString());
        if(callback)
            callback.result(event.result);
    }
    /// you can do the same with FaultEvent
    // Fault from CFC
    private function fault(event:FaultEvent){
        //Alert.show(event.fault.faultString);
        if(callback)
            callback.fault(event.fault);
    }

main class

protected function fncClick():void{
     var request:EmailListRetrieve = new EmailListRetrieve();
     request.callback = new mx.rpc.Responder(onResult, onFault);
     request.emailListId=1;
     request.send();
}

protected function onResult(item:Object):void{
     trace(item);
}

protected function onFault(item:Object):void{
     Alert.show(item, "Error");
}