问题描述
我有几个远程对象方法,我想根据上下文以不同的方式响应这些方法,但我不想设置一堆不同的 RemoteObject 别名.这样做有什么建议吗?一些背景:
I have a couple of remote object methods that I want to respond to in different ways depending on the context, but I'd rather not set up a bunch of different RemoteObject aliases. Any advice on doing that? Some background:
假设我有一个以不同方式显示销售统计数据的管理应用程序.远程方法看起来像:
Let's say I have an admin application that displays sales stats in different ways. The remote method looks like:
<mx:RemoteObject id="rpt" destination="AMFServer">
<mx:method name="getSalesStats" fault="getSalesStatsFault(event)"
result = "getSalesStatsSuccess(event)" />
</mx:RemoteObject>
getSalesStats 方法将员工 ID 和销售类型作为其参数.你可以这样称呼它:
The getSalesStats method takes an employee ID and a sales type as its arguments. You'd call it like:
rpt.getSalesStats(120, "peanuts");
public function getSalesStatsSuccess(e:ResultEvent):void {
salesdata:ArrayCollection = e.result.rows as ArrayCollection;
salesGraph.dataProvider = salesdata;
salesGraphPanel.title = "Peanut Sales, 1990";
}
我希望能够在不同的上下文中调用此方法,有时将结果发送到图表,有时发送到数据网格;我希望能够根据用户的需求更改图表的标题和类型.我想要的一些东西可以通过评估从服务器返回的数据来实现;该对象包含报告名称,因此我可以评估该值.但有些事情需要根据我从服务器返回的内容进行更改.如果这是一个同步调用,那就很容易了;我会做类似的事情:
I want to be able to call this method in different contexts, sometimes sending the result to a chart and sometimes to a datagrid; I want to be able to change the title and type of chart depending on what the user wants. Some of what I want can be achieved by evaluating the data returned from the server; the object contains the report name, so I can evaluate that value. But some things need to change based on more than just what I get back from the server. If this was a synchronous call, it would be easy; I'd do something like:
function buttonOneClick():void {
myData1:ArrayCollection = getSalesStats(120, "peanuts");
myChart.dataProvider = myData1;
}
function buttonTwoClick():void {
myData2:ArrayCollection = getSalesStats(120, "cashews");
myDataGrid.dataProvider = myData2;
}
我想通过远程方法将某些内容传递给响应函数,例如:
I'd like to pass something through the remote method to the responding function, like:
rpt.getSalesStats(120, "peanuts", "clicked button one");
但这当然会引发错误,因为服务器不想要最后一个参数.有什么想法吗?我会澄清这是否令人困惑..
but that of course throws an error because the server doesn't want that last argument. Any thoughts? I'll clarify if this is confusing..
推荐答案
在 Flex 4 和 3.4 中,使用 CallResponder 类:
In Flex 4 and 3.4, use the CallResponder class:
<mx:RemoteObject id="rpt" destination="AMFServer"/>
<s:CallResponder id="toChartResponder" fault="getSalesStatsFault(event)"
result = "getSalesStatsToChartSuccess(event)" />
<s:CallResponder id="toDataGridResponder"fault="getSalesStatsFault(event)"
result = "getSalesStatsToDataGridSuccess(event)"/>
要进行调用,请将方法调用返回的 AsyncToken 分配给响应者的 token 属性:
To make the call, assign the returned AsyncToken from the method call to the token property of the responder:
toDataGridResponder.token = rpt.getSalesStats();
这将响应定义与方法调用分开,然后您可以将其包装在您需要的任何逻辑中.
This separates the response definition from the method call, and you can then wrap it in whatever logic you need.
这篇关于Flex RemoteObject - 处理多个请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!