Flex RemoteObject - handling multiple requests

I have several remote object methods that I want to respond to differently depending on the context, but I would prefer not to create a bunch of different RemoteObject aliases. Any advice on this? Some prerequisites:

Let's say I have an admin application that displays sales statistics in different ways. The remote method is as follows:

<mx:RemoteObject id="rpt" destination="AMFServer"> <mx:method name="getSalesStats" fault="getSalesStatsFault(event)" result = "getSalesStatsSuccess(event)" /> </mx:RemoteObject> 

The getSalesStats method takes an employee identifier and a sales type as arguments. You would call it that:

 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 name 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 name of the report, so I can evaluate this value. But some things need to be changed, based not only on what I will return from the server. If it was a synchronous call, it would be easy; I would 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 would like to pass something through a remote method to the response function, for example:

 rpt.getSalesStats(120, "peanuts", "clicked button one"); 

but this, of course, causes an error because the server does not want this last argument. Any thoughts? I will clarify if this is confusing.

+4
source share
6 answers

Here's a great example of using AsyncToken with calls to the RemoteObject method to do exactly what you need.

Something to consider when using AsyncToken is that it is a dynamic object, and you can add any property that you would like to use. The event in your responder's method will contain a link to AsyncToken, and you can access your dynamic properties to easily determine the context of the response.

+2
source

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 property of the responder token:

 toDataGridResponder.token = rpt.getSalesStats(); 

This separates the definition of the response from the method call, and you can wrap it in any logic you need.

+2
source

You can have several methods for a remote object.

 <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 a reason you cannot use something like this?

0
source

Flex supports the design pattern for asynchronous completion tokens to handle multiple requests for the same service. See BlazeDS Documentation .

0
source

I think there are only two ways to do this:

  • Have a separate remote object for each call context. The effect on performance is not taken into account by IMO.
  • Set concurrency for the remote object (or, firstly, you are not sure of the name, but not several or the last), and you have some kind of flag that you can use to indicate what the last method called. This, of course, restricts server calls one at a time on this remote object. calls will immediately fail if the previous call has not yet returned a result.

This is the only way to see this if you do not have access to the server. If I were in your situation, I would even create a remote object every time I make a remote call. I do not think this affects performance (please correct me if I am wrong). Good luck

0
source

var rpcCall: AsyncToken;

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

The "remoteService" instance of remoteobject Hope this makes sense.

0
source

All Articles