Home > AMFPHP, Flash Platform, Zend_Amf > Type Mapping in AS3 : WRAP IT UP!

Type Mapping in AS3 : WRAP IT UP!

October 28th, 2008

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!

AMFPHP, Flash Platform, Zend_Amf

  1. October 28th, 2008 at 13:27 | #1

    So what is the reason to send back typed objects if you have to type them again when they get to the Flex frontend?

  2. October 29th, 2008 at 09:00 | #2

    @Garth- I think it still saves us the ceremony of having to convert the received object to the desired type property by property.

  3. October 29th, 2008 at 10:12 | #3

    @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.

  4. October 30th, 2008 at 18:47 | #4

    So for arrays of nested objects I should do a recursive function to type the children?

  5. April 19th, 2009 at 05:36 | #5

    please can you point me in the right direction for mapping the nested objects please. (any sample code would be appreciated)?

  6. April 27th, 2009 at 09:09 | #6

    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.

  7. April 27th, 2009 at 10:03 | #7

    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?

  8. May 4th, 2009 at 09:02 | #8

    In amfphp my objects weren’t nested?

  9. May 10th, 2009 at 08:12 | #9

    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 :)

  10. May 20th, 2009 at 04:21 | #10

    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

  11. May 20th, 2009 at 06:51 | #11

    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.

  1. No trackbacks yet.