Flex RemoteObject - handling multiple requests

笑着哭i 提交于 2019-12-05 13:06:05

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.

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.

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?

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:

  • 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!

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!