0

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.

  • Could you tell us more about this document? – Dan Bracuk Nov 12 '14 at 12:58
  • Document is not the proper term here (has to be another class), just dispatch a custom event and pass along the data. Also since you are english speaking "how do I can this" doesn't make any sense grammatically. – BotMaster Nov 12 '14 at 13:14
  • @Dan The document (incorrect term as pointed out), is my MXML file where I am importing the class using... import com.procost.EmailListRetrieve; then calling it using the function shown in the fncClick fuction in the original post. – David Meddings Nov 12 '14 at 13:23

2 Answers2

0

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.

Brian
  • 3,850
  • 3
  • 21
  • 37
0

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");
}
Y Borys
  • 543
  • 1
  • 5
  • 21