Flex RemoteObject - 处理多个请求
我有几个远程对象方法,我想根据上下文以不同的方式响应,但我不想设置一堆不同的 RemoteObject 别名。 这样做有什么建议吗? 一些背景:
假设我有一个管理应用程序,它以不同的方式显示销售统计数据。 远程方法如下所示:
<mx:RemoteObject id="rpt" destination="AMFServer">
<mx:method name="getSalesStats" fault="getSalesStatsFault(event)"
result = "getSalesStatsSuccess(event)" />
</mx:RemoteObject>
getSalesStats 方法采用员工 ID 和销售类型作为其参数。 您可以这样称呼它:
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";
}
我希望能够在不同的上下文中调用此方法,有时将结果发送到图表,有时发送到数据网格; 我希望能够根据用户的需求更改图表的标题和类型。 我想要的一些东西可以通过评估从服务器返回的数据来实现; 该对象包含报告名称,因此我可以评估该值。 但有些事情需要改变,而不仅仅是我从服务器返回的内容。 如果这是一个同步调用,那就很容易了; 我会做类似的事情:
function buttonOneClick():void {
myData1:ArrayCollection = getSalesStats(120, "peanuts");
myChart.dataProvider = myData1;
}
function buttonTwoClick():void {
myData2:ArrayCollection = getSalesStats(120, "cashews");
myDataGrid.dataProvider = myData2;
}
我想通过远程方法将一些东西传递给响应函数,例如:
rpt.getSalesStats(120, "peanuts", "clicked button one");
但这当然会引发错误,因为服务器不需要最后一个参数。 有什么想法吗? 我会澄清这是否令人困惑..
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>
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..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
下面是一个很好的例子,使用 AsyncToken 和 RemoteObject 方法调用来精确地完成您需要的操作。
使用 AsyncToken 时要记住的一点是,它是一个动态对象,您可以向其中添加任何您想要的属性。 响应程序方法中的事件将携带对 AsyncToken 的引用,您可以访问动态属性以轻松识别响应的上下文。
Here is a great example of using AsyncToken with RemoteObject method calls to do precisely what you need.
Something to keep in mind when you are using AsyncToken is that it is a dynamic object and you can add any property you'd like to it. The event in your responder method will carry a reference to the AsyncToken and you can access your dynamic properties to easily identify the context of the response.
在 Flex 4 和 3.4 中,使用 CallResponder 类:
要进行调用,请将方法调用返回的 AsyncToken 分配给响应者的 token 属性:
这将响应定义与方法调用分开,然后您可以将其包装在任何内容中你需要的逻辑。
In Flex 4 and 3.4, use the CallResponder class:
To make the call, assign the returned AsyncToken from the method call to the token property of the responder:
This separates the response definition from the method call, and you can then wrap it in whatever logic you need.
一个远程对象可以有多种方法。
有什么理由不能使用这样的东西吗?
You can have multiple methods to a remoteObject.
Is there any reason why you can't use something like this?
Flex 支持异步完成令牌设计模式,用于处理对同一服务的多个请求。 请参阅 BlazeDS 文档。
Flex supports the Asynchronous Completion Token design pattern for handling multiple requests to the same service. See the BlazeDS documentation.
我认为只有两种方法可以做到这一点:
如果您无法访问服务器,这是我看到它的唯一方式。 如果我处于您的情况,我什至会在每次进行远程调用时创建远程对象。 我认为这不会影响性能(如果我错了,请纠正我)。 祝你好运!
I think there are only two ways to do this:
That's the only way I see it if you have no access to the server. If I were in your situation, i will even create the remote object every time I do the remote call. I don't think it affects performance (please correct me if I'm wrong). Good luck!
var rpcCall:AsyncToken;
远程对象的“remoteService”实例
希望它有意义。
var rpcCall:AsyncToken;
"remoteService" instance of remoteobject
Hope it will make sense.