Flex RemoteObject - 处理多个请求

发布于 2024-07-26 22:43:30 字数 1355 浏览 11 评论 0原文

我有几个远程对象方法,我想根据上下文以不同的方式响应,但我不想设置一堆不同的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

缺⑴份安定 2024-08-02 22:43:31

下面是一个很好的例子,使用 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.

泪意 2024-08-02 22:43:31

在 Flex 4 和 3.4 中,使用 CallResponder 类:

<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 属性:

toDataGridResponder.token = rpt.getSalesStats();

这将响应定义与方法调用分开,然后您可以将其包装在任何内容中你需要的逻辑。

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)"/>

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.

哆兒滾 2024-08-02 22:43:31

一个远程对象可以有多种方法。

<mx:RemoteObject id="rpt" destination="AMFServer">
    <mx:method name="getSalesStatsToChart" fault="getSalesStatsFault(event)" 
        result = "getSalesStatsToChartSuccess(event)" />
    <mx:method name="getSalesStatsToDataGrid" fault="getSalesStatsFault(event)" 
        result = "getSalesStatsToDataGridSuccess(event)" />
</mx:RemoteObject>

有什么理由不能使用这样的东西吗?

You can have multiple methods to a remoteObject.

<mx:RemoteObject id="rpt" destination="AMFServer">
    <mx:method name="getSalesStatsToChart" fault="getSalesStatsFault(event)" 
        result = "getSalesStatsToChartSuccess(event)" />
    <mx:method name="getSalesStatsToDataGrid" fault="getSalesStatsFault(event)" 
        result = "getSalesStatsToDataGridSuccess(event)" />
</mx:RemoteObject>

Is there any reason why you can't use something like this?

ま柒月 2024-08-02 22:43:31

Flex 支持异步完成令牌设计模式,用于处理对同一服务的多个请求。 请参阅 BlazeDS 文档

Flex supports the Asynchronous Completion Token design pattern for handling multiple requests to the same service. See the BlazeDS documentation.

倾`听者〃 2024-08-02 22:43:31

我认为只有两种方法可以做到这一点:

  • 为每个调用上下文都有一个单独的远程对象。 在我看来,对性能的影响可以忽略不计。
  • 将远程对象的并发设置为单个(或者首先,不确定名称,但不是多个或最后一个),并具有某种标志,您可以使用它来判断最后调用的方法是哪个。 当然,这将限制服务器对该远程对象的调用一次只能调用一个。 如果前一个调用尚未返回结果,则调用将立即失败。

如果您无法访问服务器,这是我看到它的唯一方式。 如果我处于您的情况,我什至会在每次进行远程调用时创建远程对象。 我认为这不会影响性能(如果我错了,请纠正我)。 祝你好运!

I think there are only two ways to do this:

  • Have a separate remote object for each call context. The effect on performance is neglectable IMO.
  • Set concurrency for the remoteobject to single (or first, not sure about the name, but not multiple or last) and have some sort of flag that you can use to tell which was the last method called. This will, of course, limit server calls to one at a time on this remote object. calls will fail immediately if the previous call didn't return a result yet.

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!

无声情话 2024-08-02 22:43:31

var rpcCall:AsyncToken;

    rpcCall = remoteService.getSessionId();
    rpcCall.addResponder(new Responder(handler_getSessionIdSuccess, handler_getSessionIdFault) );

    rpcCall = remoteService.getMyData();
    rpcCall.addResponder(new Responder(handler_getMyDataSuccess, handlerfault));

远程对象的“remoteService”实例
希望它有意义。

var rpcCall:AsyncToken;

    rpcCall = remoteService.getSessionId();
    rpcCall.addResponder(new Responder(handler_getSessionIdSuccess, handler_getSessionIdFault) );

    rpcCall = remoteService.getMyData();
    rpcCall.addResponder(new Responder(handler_getMyDataSuccess, handlerfault));

"remoteService" instance of remoteobject
Hope it will make sense.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文