6 Replies Latest reply on Sep 25, 2006 6:34 PM by lenny2

    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
          Renaun_Erickson
          It probably has to do with your Collection and how the objects are typed when you create it. Here is some sample code that you should be able to gleam what you need.

          [Bindable]
          [ArrayElementType("com.renaun.samples.vo.UserVO")]
          private var users:ArrayCollection;

          private function resultHandler( event:ResultEvent ):void
          {
          users = new ArrayCollection( ArrayUtil.toArray( event.result ) );
          }

          private function viewDetails():void
          {
          lblUser.text = UserVO( dgUsers.selectedItem ).username;
          }
          • 2. Re: selectedIndex and Value Objects
            lenny2 Level 1
            First, thanks for the response... I can't even get past this part:
            users = new ArrayCollection( ArrayUtil.toArray( event.result ) );

            the only way I can use the results as the dataprovider for my grid was to do this:
            private var myGridDP:ArrayCollection;
            and then in the result handler:
            myGridDP = event.result as ArrayCollection;

            Maybe because I'm use a cfquery as the results...doesn't that automaitcally return an ArrayCollection?

            Anyway my grid is populating just fine. And I can get the seletedItem data to show up in the details for if I explicitly assign the data as such: myForm.first_name.text = myGrid.selectedItem. first_name;

            But I would rather populate my vo. So maybe my vo class is not defined properly?? I do not know what this does:
            [ArrayElementType("com.renaun.samples.vo.UserVO")]

            Also, I notice that you are passing the selectedItem object into the constructor of the class...do have code in the contructor to handle the data?
            • 3. Re: selectedIndex and Value Objects
              lenny2 Level 1
              DOH!!! forget the last part of my post..I jsut realized that you are merely casting the selectedItem object to UserVO. But for me this code : oEmployee = Employee(myGrid.selectedItem) throws the coercion error. For some reason I cannot cast the selectedItem object to type Employee.
              • 4. Re: selectedIndex and Value Objects
                Renaun_Erickson Level 1
                yes thats the issue, you can't cast it because it is not that object. If you pass back an array typed objects then the casting will work. But with the cfquery you get the ArrayCollection and no typed objects. You will either have to pre-process it on the CFC side or pre-process it on the Client side before you set the ArrayCollection.

                • 5. Re: selectedIndex and Value Objects
                  Renaun_Erickson Level 1
                  You answered your own question with "Maybe because I'm use a cfquery as the results...doesn't that automaitcally return an ArrayCollection?"

                  cfquery results are passed back as ArrayCollection of objects. If you want to use typed objects will have to type them on the backend and send them as an Array of typed objects.

                  quote:

                  But I would rather populate my vo. So maybe my vo class is not defined properly?? I do not know what this does:
                  [ArrayElementType("com.renaun.samples.vo.UserVO")]


                  This defines the items in an ArrayCollection to be a certain type, in this case typed as "com.renaun.samples.vo.UserVO".

                  quote:

                  Also, I notice that you are passing the selectedItem object into the constructor of the class...do have code in the contructor to handle the data?


                  Its not a constructor but casting a object as a certain type. Same as "oEmployee = Employee(myGrid.selectedItem)".
                  • 6. Re: selectedIndex and Value Objects
                    lenny2 Level 1
                    I looped over the results set in the cfc and created an array of Employee object instances and passed that back to flex. Now this bit of magic in my resutlsGrid code: oEmployee = resultsGrid.selectedItem as Employee populates all the properties of the flex Emlpoyee object (Employee.as). I have no idea how this works as the Emlpoyee.as class file has no getters or setters at all????

                    But early man learned quickly not to question fire either, eh?