4 Replies Latest reply on Sep 14, 2006 11:25 AM by KomputerMan.com

    Using a DataGrid to get a RemoteObject

    KomputerMan.com Level 1
      What I have is a form comprised of two components. One component is a datagrid that contains a list of users in it. The other component is just a bunch of checkboxes.

      What I need to be able to do is select a user from the datagrid and have the second component fire off a RemoteObject call and return the data about the person I selected in the datagrid. What I need to know is how do I pass the selected users ID value to the RemoteObject call??? The ID of the selected user is contained in a Object called SelUserInfo.UserCnt. This object gets sent to the second component when something gets selected from the data grid. So far this is what I have for my code..

      My RemoteObject call:
      <mx:RemoteObject id="getUserSec" destination="ColdFusion" source="ISIF.WebServices.Flex.UserData" ShowBusyCursor="true">
      <mx:method name="getUserSecRoles" result="getRolesHandler(event)">
      <mx:arguments>
      <UserCnt>{SelUserInfo.UserCnt}</UserCnt>
      </mx:arguments>
      </mx:method>
      </mx:RemoteObject>

      My handler for the RemoteObject call:
      [Bindable]
      private var userSecRoles:ArrayCollection;

      private function getRolesHandler(event:ResultEvent):void
      {
      userSecRoles = new ArrayCollection(event.result as Array);
      }


      My function that gets called when a user is selected from the datagrid:
      public function getUserRoles() : void
      {
      getUserSec.getUserSecRoles.send();
      }

      The problem is that the userSecRoles array is always null. Anybody know if I am calling the RO correctly???

      Have an Ordinary Day...
      KomputerMan ~|:-)
        • 1. Re: Using a DataGrid to get a RemoteObject
          peterent Level 2
          I personally don't like data binding with RemoteObject as I prefer to invoke the remote methods as if they were regular function calls. So in your case it would be:

          getUserSec.getUserSecRoles(SelUserInfo.UserCnt); -- just pass the arguments in the order they are expected

          When you select a row from the DataGrid a change event is fired. In that event handler you can get the data in the row:
          event.target.selectedItem will be the Object displayed in that row.

          If the ID of the person is part of that data (eg, userID), then in the change event handler on the DataGrid you could do:

          getUserSec.getUserSecRoles( event.target.selectedItem.userID );

          I don't know what your row data looks like, but that's the basic idea.


          • 2. Using a DataGrid to get a RemoteObject
            KomputerMan.com Level 1
            Thats pretty simple! I think part of the problem I am having though is in how I am calling the remote object. If I make the call like the following I get data back like I expect:
            <?xml version="1.0" encoding="utf-8"?>
            <mx:VBox xmlns:mx=" http://www.adobe.com/2006/mxml" borderStyle="solid" borderThickness="3" paddingBottom="10"
            paddingLeft="10" paddingRight="10" paddingTop="10" creationComplete="getUserSec.getUserSecRoles(2)">

            If I call it from my event handler though I don't. Any ideas on why this call fails when the exact same call works if called from the header tag???

            public function getUserRoles():void
            {
            getUserSec.getUserSecRoles(2);
            }

            ****************************EDIT****************************
            O.K. so the problem is the first time I clik on a user from the data grid nothing happens but the second time I click on something in the data grid it works. Why would the first click not do anything but the second clik kick off the RO call correctly???

            Have an Ordinary Day...
            KomputerMan ~|:-)

            • 3. Re: Using a DataGrid to get a RemoteObject
              peterent Level 2
              There should be absolutely no reason why it would not work unless the data it is given is wrong. I would use the debugger and set a breakpoint in the change handler on the DataGrid and examine the contents of the selected item:

              private var changeHandler( event:flash.event.Event ) : void {
              var item:* = event.target.selectedItem;
              // use debugger to see what item really is
              }

              If that looks OK, then use a network monitor, like Charles, to see what is leaving the client and heading out to the server.
              • 4. Re: Using a DataGrid to get a RemoteObject
                KomputerMan.com Level 1
                Problem turned out to be in the order that things were getting called. I would call the datagrid listener when a user was selected using this code:

                [Bindable]
                private var selectedUser2:Object;

                private function userSelHandler2(event:KDL_SelEvent):void
                {
                selectedUser2 = event.KDL_SelObject;
                getUserSec.getUserSecRoles(selectedUser2.UserCnt);
                Roles2TempID.getUserRoles(); // initialize the checkboxes
                }

                The getUserSec.getUserSecRoles(selectedUser2.UserCnt); line would call my RemoteObject which called the following event handler to put the data from the RO into an array.

                [Bindable]
                private var myUserSecRoles:ArrayCollection;

                private function getRolesHandler(event:ResultEvent):void
                {
                myUserSecRoles = new ArrayCollection(event.result as Array);
                }

                Problem is that the Roles2TempID.getUserRoles(); line of my datagrid selected event would fire off before the RO call was finished. That way I didn't have any data when the template was being drawn. To solve this problem I call the code to initialize my template after my RO handler gets finished like this:

                [Bindable]
                private var myUserSecRoles:ArrayCollection;

                private function getRolesHandler(event:ResultEvent):void
                {
                myUserSecRoles = new ArrayCollection(event.result as Array);
                Roles2TempID.getUserRoles(); // initialize the checkboxes
                }

                Now the data gets passed to the template before the template itself gets initialized. My guess is I have to do it this way because of how Flex does its threading. Just a guess mind you. But it works now!

                Have an Ordinary Day...
                KomputerMan ~|:-)