Flex 3 动态远程对象类
我有一个类,我想在我的项目中使用它。 它本质上允许我轻松地使用 RemoteObject,这样我就不必在我的所有项目中定义它。 当不将“args”传递给 sendRequest(..) 时它会起作用。 但是,当我想使用参数调用 cfc 函数并尝试传递“args”时,出现以下错误:
函数 getAllPreferences 需要参数 USERNAME 但没有传入。
这是我的代码:
package Actionscript
{
import mx.collections.ArrayCollection;
import mx.rpc.AbstractOperation;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
import mx.rpc.remoting.mxml.RemoteObject;
public class CFCRemote
{
private var ro:RemoteObject;
private var roSource:ArrayCollection;
private var appPointer:Object;
// constructor
public function CFCRemote(appMain:Object):void
{
appPointer = appMain;
roSource = new ArrayCollection();
}
public function addSource(alias:String, source:String, thisObj:Object):void
{
roSource.addItem({alias:alias, source:source, thisObj:thisObj});
}
public function sendRequest(roAlias:String, funcName:String, args:Object = null):void
{
var roCaller:Object;
ro = new RemoteObject("ColdFusion");
ro.showBusyCursor = true;
for(var i:int = 0; i < roSource.length; i++)
{
if(roSource.getItemAt(i).alias == roAlias)
{
ro.source = roSource.getItemAt(i).source;
roCaller = roSource.getItemAt(i).thisObj;
break;
}
}
var ao:AbstractOperation = ro.getOperation(funcName);
ao.arguments = args;
ao.addEventListener(ResultEvent.RESULT, function(e:ResultEvent):void{roCaller.handleROF(e,funcName)});
ao.addEventListener(FaultEvent.FAULT, function(e:FaultEvent):void{appPointer.server_fault(e,funcName,"unknown")});
ao.send();
}
}
}
当我跟踪(args.username)时,我看到它正确传入。 我在另一个脚本中尝试了此操作,但是该脚本中的 RemoteObject 是使用 mxml 定义的。 我不明白这会有什么不同。
没有想法了:(
编辑:它现在对我们有用,如果有人想使用这个解决方案并且在实现这个类时遇到问题,我很乐意提供帮助
I have a class which I want to use throughout my projects. It essentially would allow me to easily work with a RemoteObject so that I don't have to define it throughout all of my projects. It works when not passing "args" to sendRequest(..). But when I want to call the cfc function with parameters and try passing "args" in I get the following error:
The parameter USERNAME to function getAllPreferences is required
but was not passed in.
Heres my code:
package Actionscript
{
import mx.collections.ArrayCollection;
import mx.rpc.AbstractOperation;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
import mx.rpc.remoting.mxml.RemoteObject;
public class CFCRemote
{
private var ro:RemoteObject;
private var roSource:ArrayCollection;
private var appPointer:Object;
// constructor
public function CFCRemote(appMain:Object):void
{
appPointer = appMain;
roSource = new ArrayCollection();
}
public function addSource(alias:String, source:String, thisObj:Object):void
{
roSource.addItem({alias:alias, source:source, thisObj:thisObj});
}
public function sendRequest(roAlias:String, funcName:String, args:Object = null):void
{
var roCaller:Object;
ro = new RemoteObject("ColdFusion");
ro.showBusyCursor = true;
for(var i:int = 0; i < roSource.length; i++)
{
if(roSource.getItemAt(i).alias == roAlias)
{
ro.source = roSource.getItemAt(i).source;
roCaller = roSource.getItemAt(i).thisObj;
break;
}
}
var ao:AbstractOperation = ro.getOperation(funcName);
ao.arguments = args;
ao.addEventListener(ResultEvent.RESULT, function(e:ResultEvent):void{roCaller.handleROF(e,funcName)});
ao.addEventListener(FaultEvent.FAULT, function(e:FaultEvent):void{appPointer.server_fault(e,funcName,"unknown")});
ao.send();
}
}
}
When I trace(args.username) I see that its passed in correctly. I tried this in another script, however, the RemoteObject in that script was defined in with mxml. I don't see how this would be much different.
Running out of ideas :(
Edit: It's working for us now, if anyone wants to use this solution and is having a problem implementing this class I'll gladly help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试将参数传递给 AbstractOperation.send() 而不是将它们设置在 AbstractOperation 上。 我之前没有尝试过后者,但我已经将它们传递给 send() 方法多次,我可以毫无问题地进行计数。 您可能需要考虑使用“...args”或作为 Array/ArrayCollection 传递参数,以确保维持顺序。
Trying passing the arguments to AbstractOperation.send() rather than setting them on the AbstractOperation. I haven't tried the latter before but I've passed them to the send() method more times that I can count without problems. You may want to consider passing the args using "... args" or as an Array/ArrayCollection to ensure you maintain the order.
好吧,我不能说我非常了解 Flex,但实例化抽象类这一事实确实给我带来了一些危险信号。 通常,您从不直接实例化抽象类 - 相反,它们被用作您实际实例化的类的基类。
在这种情况下,我认为您正在寻找操作类。 如果是这种情况,那么您可能还需要为其提供一个“argumentNames”数组 - 这似乎提供了参数传递的顺序。
Well I can't say I know Flex terribly well, but the fact that you're instantiating an abstract class does throw up some red flags for me. Usually you never directly instantiate abstract classes - instead they are used as base classes for the classes you actually do instantiate.
In this case I think you are looking for the Operation class. If that's the case then you may also need to provide it with an "argumentNames" array - this seems to provide the order in which your arguments should be passed.