我有一个使用ColdFusion的Flex应用程序从MS SQL检索数据。我试图创建一个类,我可以在其中发送一个数字参数,并向调用该类的文档返回一个值。
这是我的课程
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());
}
}
}
**这就是我从我的MXML **
中调用它的方式import com.procost.EmailListRetrieve;
public function fncClick():void{
var request:EmailListRetrieve = new EmailListRetrieve();
request.emailListId=1;
request.send();
}
我班级的结果函数是从数据库返回我需要的所有数据。问题是,如何将这些数据反馈到我从中调用的MXML文档?
任何帮助将不胜感激。
答案 0 :(得分:0)
您需要做的就是在另一个事件中回复ResultEvent
的数据(请注意,您希望自定义事件类来传输数据):
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()));
}
}
然后,在您的MXML代码中:
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
}
重要提示:您的request
EmailListRetrieve
对象可能会在收到回复之前收集垃圾。我建议在MXML中的类级变量中保存对它的引用,而不是让它成为函数成员。
答案 1 :(得分:0)
您可以使用Responder将数据发送回主类。
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);
}
主要班级
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");
}