6 Replies Latest reply on Sep 25, 2006 8:54 PM by inlineblue

    selectedIndex and Value Objects

    lenny2
      Please Please Please!!! been searching and trying a million things for days and can't figure this out....

      In a custom container I have a data grid custom component that displays results from a remote object call to a cfc. When the user clicks on an item I'd like to populate form items in a form container that resides in the custom container (i.e. NOT a a seperate custom component). I've created a value object class called Employee (actually the CF wizard did) in a file called Employee.as.

      In the custom container I import the employee class as such: import comp.Employee.

      In the initialize event handler of the custom container I create an instance of the employee class as such:
      oEmployee = new Employee();

      In the click event handler of the custom datagrid component I want to populate the oEmployee object with the record the user clicked on as such: oEmployee = myGrid.selectedItem as Employee.

      The above code compiles just fine. But when I click on the grid I get the "null object has no properties" error. This didn';t tell me much so I changed the code to use the AS2 casting code as such:
      oEmployee = Employee(myGrid.selectedItem)

      This also compiles just fine put ar run time throws this error:
      Type Coercion failed: cannot convert Object@9a00b01 to comp.Employee.

      I thought I saw examples somewhere of someone passing a selectedItem object to a custom class but I cannot get it to work. Must I write code like such: oEmployee.first_name = myGrid.selectedIndex.first_name?? My VO has a lot of data in it and this would be tedious. I could just bypass the whole vo thing and simple write:
      myFormItem.first_name = myGrid.selectedItem.first_name but a) that doesn';t promote loose coupling between generic components and b) I would like to use the data in the oEmployee vo to pass to a cfc in a save method.

      Thanks in advance for any help on this!!


        • 1. Re: selectedIndex and Value Objects
          ntsiii Level 3
          Do the grid's dataProvider items contain Employee instances?

          Tracy
          • 2. Re: selectedIndex and Value Objects
            lenny2 Level 1
            Tracy,,thanks so much for your response... this is killing me...
            No they do not. The data is coming from a call to a CFC which just returns the CF query object. This is how I am populating the data grid

            [Bindable]
            public var dmData:ArrayCollection;

            private function pageInit():void {
            dmData = new ArrayCollection();
            }

            public function getDataResultHandler(event:ResultEvent):void {
            dmData = event.result as ArrayCollection;
            CursorManager.removeBusyCursor();
            }

            And this is my grid component:
            <MyComp:XMLDataGrid id="resultsGrid" height="100%" width="100%" dataProvider="{dmData}" click="populateDetailsPanel();" />
            • 3. Re: selectedIndex and Value Objects
              ntsiii Level 3
              I don't do CF, so I don't know what magic the wizard is supposed to do.

              What AS data structure do you actually have in the result of the remoteObject call?

              Tracy
              • 4. Re: selectedIndex and Value Objects
                ntsiii Level 3
                You can only cast if an object contains the kind of object you are trying to cast to.
                • 5. Re: selectedIndex and Value Objects
                  lenny2 Level 1
                  Thanks, Tracy. Actually your first response helped me solve the issue. It triggered something that I read about the nature of the object that ColdFusion returns as a query object. While this object can be cast as an ArralyCollection and used as the data provider in a data grid, the selected item object that it produces is of a very generic nature and can't be cast. So in my ColdFusion CFC, I looped over my query results and converted the query object into an array of Employee objects with each element in the array corresponding to each row in the result set represented by an instantiation of my Employee.cfc (vaue object). This array of Employee instances is what gets passed back to Flex. In Flex then I just create a bound ArrayCollection whose source attribute is the returned aray (casting it to a Flex array for good measure) which is used ast the data provider for my grid. Now when a user click on a record in th grid, the selectedItem object is of type = Emloyee and the eventhandler does this: oEmployee = resultsGrid.selectedItem as Employee. As long as the ColdFusion Employee.cfc and the flex Emloyee.as class definitions are identical (with regard to properties), oEmloyee will be magically populated with the data from the selectedItem object.

                  I have no iea how this works as my Emloyee.as class file has no getters or setters defined. But then again, early man learned quickly not to quesiton fire either, eh?


                  • 6. Re: selectedIndex and Value Objects
                    inlineblue Level 1
                    Flex has the capability to map server-side classes to client-side classes. I've never used the CF wizard, but I'm guessing it inserted a RemoteClass clause at some point to map Employee.as to the remote Employee.cfc object. If the mapping is done correctly, the RemoteObject will actually create instances of the Employee class when deserializing the result set. It looks like the dataprovider was actually an array of Employee objects, which is why the cast worked.