0 Replies Latest reply on Jul 1, 2010 1:00 PM by WolffenTCI

    Flex3: Parsing Remote Object event.result from CF as class/object

    WolffenTCI

      I'm used to primarily dealing with calling and parsing data from WSDLs/SOAP webservices in Flex, and I've been trying to work with Remote Object and AMF via ColdFusion/Flex integration in Flex 3. I've created an Employee object/ActionScript class which holds all my properties for an Employee, and I am able to pass the Employee object as a parameter to a CFC successfully (for adding/updating an employee record). I would like to be able to pass back an object/struct from ColdFusion as the CFC result of the getter CFC method to Flex and cast that event.result as my Employee object. My hope was that I could simply do something like this:

       

      private function resultHandler(event:ResultEvent):void {

       

           var myEmp:Employee = event.result as Employee;

           trace(myEmp.fname);

       

      }

       

      but I'm getting a null pointer error. The myEmpinstantiation doesn't throw an error, but the object properties are obviously not being set/parsed when I cast the event.result as Employee. Is what I'm trying to do even possible with Flex and CF, or will I need to set the properties of the myEmp variable one by one (myEmp.fname = event.result.fname)?

       

      My very basic Employee.as class:

       

      package

      {

           [Bindable]

           public class Employee

           {

                public var fname:String = "";

                public var lname:String = "";

                public var empnum:String = "";

       

                public function Employe() {

                }

           }

      }

       

      emp.cfc:

       

      <cfcomponent>

           <cffunction name="getemp" access="remote" returntype="struct">

                <cfset theEmp = structnew()>

       

                <cfset theEmp["fname"] = "Joe">

                <cfset theEmp["lname"] = "Smith">

                <cfset theEmp["empnum"] = "012345">

                <cfreturn theEmp>

           </cffunction>

      </cfcomponent>

       

       

      Very basic test.MXML file:

       

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" initialize="init();" width="800" height="600">

          <mx:Script>
              <![CDATA[

       

                  import mx.rpc.events.FaultEvent;
                  import mx.rpc.events.ResultEvent;

       

                  private function init():void {
                      addEmpws.getemp();
                  } 

       

                 private function resultHandler(event:ResultEvent):void {

                     var myEmp:Employee = event.result as Employee;

                     trace(myEmp.fname);

                }

       

              ]]>
          </mx:Script>

       

          <mx:RemoteObject id="addEmpws" destination="ColdFusion" source="emps"
               result="resultHandler(event)" endpoint="http://localhost/flex2gateway/">
              <mx:method name="getemp"  />
          </mx:RemoteObject>

       

       

      </mx:Application>