Type Mapping in AS3 : WRAP IT UP!
Type Cast objects coming back in AMF are mx.utils::ObjectProxy and are capable of becoming the object type that you specified. However we don’t want to JUST ASSume that you are getting the correct object type back. It takes a small amount of work. In order to change the ObjectProxy into the proper data type.I recommend that you wrap your result into a try catch before you just assume that you have received the correct data.
private function onResult(event:ResultEvent):void{
try {
var myContact:ContactVO = event.result as ContactVO;
trace('name' +myContact.firstname);
}
catch(error: Error) {
trace('onResult error: ' + error.message);
}
}
Yes you could just Jam the type into a value.
var myContact:ContactVo = ContactVO(event.result);
But if something didn’t work out on the server and you get a successful result your application just crashed because you have a type correlation exception.
or as Ivan Erickson Suggested:
private function onResult(event:ResultEvent):void{
try {
var myContact:ContactVo = ContactVO(event.result);
trace('name' +myContact.firstname);
}
catch(error: Error) {
trace('onResult error: ' + error.message);
}
}
if you’re not going to null check the return from var and save the CPU cycle for the as().. thanks Ivan!
Joel Caspers a new T8DESIGN stud also says:
Also, you can remove the need for try-catch statement entirely by using the is operator in an if statement – in my experience try catch statements are processor costly and I’ve always been instructed to stay away from them, as well as you could be catching an error that you are not intending to catch (something other than the cast error inside your try statement).
If (event.result is ContactVO) {
//it is what you think it is
Var myContact:ContactVO = ContactVO(event.result);
} else {
//the result is not what you think it is so spit out the type in the console
Trace(getQualifiedClassName(event.result));
}
Hope that this helps!


So what is the reason to send back typed objects if you have to type them again when they get to the Flex frontend?
@Garth- I think it still saves us the ceremony of having to convert the received object to the desired type property by property.
@garth Because you would have to iterate through the returned result and apply it to a new ContactVO object. If you used the above code without class mapping you would get an error:
Error #1009: Cannot access a property or method of a null object reference.
You would have to do things like
myContactVO.firstname = event.result.firstname;
myContactVO.lastname = event.result.lastname;
So this could be done on a pretty flat object such as what we have here… EXAMPLE… however once you start having nested objects in your data structure of different typed objects it becomes really beneficial.
So for arrays of nested objects I should do a recursive function to type the children?
please can you point me in the right direction for mapping the nested objects please. (any sample code would be appreciated)?
I’d like a final answer to this: Is there no way to have flex automatically type cast the received object?
In charles I see that my objects are being returned as the correct class.
Also I can’t seem to use typecasting, I get an object back like this: http://i44.tinypic.com/zv4il5.jpg
Why is there a second object nested inside the ObjectProxy, should that be there?
In amfphp my objects weren’t nested?
Alright, so I found the answer myself. In order for me to get correct classmapping I had to map like this in my AS class: http://pastebin.com/f1ee02071 and like this in my php class: http://pastebin.com/f6d021f18
Now I get correct type mapping in both charles and flex
huumm i wish someone had helped us hear, i am still struggling to get a nested array to return it always returns as null, please help
ok ive fixed it create an ArrayCollection class in php then send this back to flex, its worked for me, hope that helped you Lasse Moos.