Reputation: 1182
I have a service which returns an Array of ObjectProxy objects. I would like to cast this to a custom object (a value object) and create an ArrayCollection. How can I do this?
Edited:
I am using Django and PyAMF for the backend. I had to write a custom SQL query and I am wrapping the resulting records in ObjectProxy and sending the whole result as an ArrayCollection.
Here is my client side code:
[ArrayElementType("SessionVO")]
[Bindable]
private var _list:ArrayCollection;
private function onSessionResultSuccess(event:ResultEvent):void
{
_list = new ArrayCollection(event.result as Array));
}
When I debug, I notice that the elements of event.result are of type ObjectProxy but the _list variable is null. Is there another than to loop over event.result and copy them into _list as SessionVO objects?
Upvotes: 0
Views: 1954
Reputation: 2122
make makeObjectsBindable="false" in the web service property, it will return object.
<s:WebService id="ws" wsdl="http://`xxxx/mobiledata/MobileDataService.asmx?wsdl" fault="fault(event)">
<s:operation
name="GetAll"
resultFormat="object"
result="GetData(event)" makeObjectsBindable="false"
/>
<s:operation
name="Create"
resultFormat="object"
result="SaveData(event)"
/>
</s:WebService>
Upvotes: 0
Reputation: 451
You can use com.adobe.serializers.utility.TypeUtility;
public function result_handler(event:ResultEvent):void{
var result:Array = TypeUtility.convertListToStrongType(event.result,YourValueObject) as Array;
}
Upvotes: 0
Reputation: 373
If you use the [RemoteClass] tag on your value objects, Flex remoting (Blaze, LCDS) will convert them to your value object for you when sending/returning from a remote service call.
The syntax for RemoteClass is
[RemoteClass(alias="com.co.custom.remote.class")] <--- point to the remote java/php class def
Public Class FooBar
{
public instance variable;
}
Your service can then return an array or hashtable of this class and Flex remoting will convert it for you. There are cases where objects can become opaque, which you may need to create ObjectProxy code to do custom marshaling, but this is not common. The RemoteClass marshaling can handle very ccomplex object types, subtypes, embedded objects in objects, etc. As long as all of objects on the AS side are typed with RemoteClass, it works as expected.
Upvotes: 2
Reputation: 39408
I'm not sure what you mean by "ObjectProxy objects." I sounds to me like you are already getting returned an array of custom objects.
I recommend looking into some form of AMF gateway. Most serer side languages have an AMF add-on piece to them. It is built into ColdFusion, BlazeDS, and LiveCycle. PHP has ZendAMF and AMFPHP. .NET has FlourineFX and WebORB. Those are just a few examples.
AMF Gateways have a automatic conversion facility; so that the server side object can easily map to a client side object. Here is some info on the RemoteObject tag that describes this. Basically, you specify the RemoteClass metadata on your client side object, and usually specify some form of mapping on the server side object. The AMF Gateway magically handles the rest of the conversion.
In your RemoteObject result handler, you'll just have to convert the returned array to an ArrayCollection. Usually something like this:
var resultArray : Array = event.result as Array;
var resultCollection : ArrayCollection = new ArrayCollection(resultArray);
If you have no control over the server side piece of this application, you may be stuck looping over the results and manually converting them into client side Flex objects.
Upvotes: 0