5 Replies Latest reply on Dec 14, 2008 4:21 PM by Newsgroup_User

    problem with my selectedItem

    hallik
      I have a project I am working on trying to teach myself flex. I have gotten it to read from a database and display a list of users in the data grid. When I click on a user, I want it to display more information in a User Details Panel. My selectedItem is not being populated. Everything looks fine to me, but I am obviously missing something?

      Here is my ssoManager.mxml

      <?xml version="1.0" encoding="utf-8"?>
      <mx:VBox xmlns:mx=" http://www.adobe.com/2006/mxml" width="100%" height="100%"
      creationComplete="init()">
      <mx:Script>
      <![CDATA[
      import mx.controls.Alert;
      import mx.utils.ArrayUtil;
      import com.rmg.sso.UserAccount;
      import mx.collections.ArrayCollection;


      [Bindable] private var userCollection:ArrayCollection;
      [Bindable] private var selectedUser:UserAccount;

      private var connection:NetConnection;
      private var gateway:String = " http://localhost/userManager/amfphp/gateway.php";

      private function init():void
      {
      connection = new NetConnection();
      connection.connect( gateway );
      getUsers();
      }


      private function getUsers():void
      {
      connection.call( "UserAcctService.getUsers",
      new Responder( onResult_users, onFault ) );
      }

      private function onResult_users( data:Object ):void
      {
      userCollection = new ArrayCollection( ArrayUtil.toArray( data ) );
      }

      private function onFault( data:Object ):void
      {
      Alert.show( "Service Error" );
      }

      private function saveUser():void
      {
      if ( selectedUser == null )
      {
      //Create new
      var newC:UserAccount = new UserAccount();
      newC.userID = 0;
      newC.firstName = txt_fname.text;
      newC.lastName = txt_lname.text;
      newC.email = txt_email.text;

      connection.call( "UserAcctService.saveUser",
      new Responder( onResult_save, onFault ), newC );
      } else {
      //Update old
      var updateC:UserAccount = new UserAccount();
      updateC.userID = selectedUser.userID;
      updateC.firstName = txt_fname.text;
      updateC.lastName = txt_lname.text;
      updateC.email = txt_email.text;


      connection.call( "UserAcctService.saveUser",
      new Responder( onResult_update, onFault ), updateC );
      }
      }

      private function onResult_update( data:Object ):void
      {
      trace( data );
      getUsers();
      }

      private function onResult_save( data:Object ):void
      {
      trace( data );
      getUsers();
      }

      private function selectHandler( event:Event ):void
      {
      //PROBLEM SEEMS TO BE HERE; NOTHING POPULATED IN selectedUser
      selectedUser = event.target.selectedItem as UserAccount;
      }

      private function onResult_delete( data:Object ):void
      {
      trace( data);
      getUsers();
      }

      private function removeUser():void
      {
      if ( selectedUser != null )
      {
      connection.call( "UserAcctService.removeUser",
      new Responder( onResult_delete, onFault ), selectedUser.userID );
      }
      }

      private function clearUser():void
      {
      selectedUser = new UserAccount();
      dg_users.selectedItem = null;

      txt_fname.text = "";
      txt_lname.text = "";
      txt_email.text = "";
      }

      ]]>
      </mx:Script>

      <mx:ApplicationControlBar width="100%">
      <mx:Image source="com/rmg/sso/logo.gif"/>
      <mx:Label text="Administration"
      fontSize="18"
      fontWeight="bold"/>
      <mx:Spacer width="100%"/>

      <mx:Button label="Refresh"
      click="getUsers()"/>

      <mx:Button label="Remove"
      click="removeUser()"/>
      </mx:ApplicationControlBar>

      <mx:HDividedBox width="100%" height="100%">
      <mx:Panel title="User Details"
      width="100%"
      height="100%"
      layout="horizontal" cornerRadius="10">

      <mx:Form width="100%" height="100%" cornerRadius="10">
      <mx:FormHeading label="User Information"/>

      <mx:FormItem label="ID:" width="100%">
      <mx:Label text="{ selectedUser.userID }"/>
      </mx:FormItem>

      <mx:FormItem label="First Name:" width="100%" required="true">
      <mx:TextInput id="txt_fname"
      text="{ selectedUser.firstName }" width="100%"/>
      </mx:FormItem>

      <mx:FormItem label="Last Name:" width="100%" required="true">
      <mx:TextInput id="txt_lname"
      text="{ selectedUser.lastName }" width="100%"/>
      </mx:FormItem>


      <mx:FormItem label="Email:" width="100%" required="true">
      <mx:TextInput id="txt_email"
      text="{ selectedUser.email }" width="100%"/>
      </mx:FormItem>

      <mx:FormItem label="" direction="horizontal" width="100%">
      <mx:Button label="Clear"
      click="clearUser()"/>
      <mx:Button label="Save"
      click="saveUser()"/>
      </mx:FormItem>
      </mx:Form>


      </mx:Panel>
      <mx:Panel title="Users"
      width="100%"
      height="100%"
      layout="vertical" alpha="1.0">

      <mx:DataGrid id="dg_users"
      width="100%"
      height="100%"
      change="selectHandler( event )"
      dataProvider="{ userCollection }">

      <mx:columns>
      <mx:DataGridColumn headerText="ID" dataField="userID" width="50"/>
      <mx:DataGridColumn headerText="First Name" dataField="firstName"/>
      <mx:DataGridColumn headerText="Last" dataField="lastName"/>
      <mx:DataGridColumn headerText="Email" dataField="email"/>
      </mx:columns>
      </mx:DataGrid>
      </mx:Panel>
      </mx:HDividedBox>
      </mx:VBox>


      And here is my UserAccount.as


      package com.rmg.sso
      {
      [RemoteClass(alias="com.rmg.sso.UserAccount")]

      [Bindable]
      public class UserAccount
      {
      public var userID:int;
      public var firstName:String;
      public var lastName:String;
      public var email:String;
      public var created_at:String;
      public var updated_at:String;
      public var isActive:String;
      public var spName:String;
      public var spIsActive:String;

      public function UserAccount()
      {
      }

      }
      }


      I have tested the php code using amfphp, and the backend works fine; it's sending the data, but I cannot figure out why it won't render when I select a row. Please help me. :(
        • 1. Re: problem with my selectedItem
          Level 7

          "hallik" <webforumsuser@macromedia.com> wrote in message
          news:gi2enu$f90$1@forums.macromedia.com...
          >I have a project I am working on trying to teach myself flex. I have gotten
          >it
          > to read from a database and display a list of users in the data grid.
          > When I
          > click on a user, I want it to display more information in a User Details
          > Panel.
          > My selectedItem is not being populated. Everything looks fine to me, but I
          > am
          > obviously missing something?
          >
          > Here is my ssoManager.mxml
          >
          > <?xml version="1.0" encoding="utf-8"?>
          > <mx:VBox xmlns:mx=" http://www.adobe.com/2006/mxml" width="100%"
          > height="100%"
          > creationComplete="init()">
          > <mx:Script>
          > <![CDATA[
          > import mx.controls.Alert;
          > import mx.utils.ArrayUtil;
          > import com.rmg.sso.UserAccount;
          > import mx.collections.ArrayCollection;
          >
          >
          > [Bindable] private var userCollection:ArrayCollection;
          > [Bindable] private var selectedUser:UserAccount;
          >
          > private var connection:NetConnection;
          > private var gateway:String =
          > " http://localhost/userManager/amfphp/gateway.php";
          >
          > private function init():void
          > {
          > connection = new NetConnection();
          > connection.connect( gateway );
          > getUsers();
          > }
          >
          >
          > private function getUsers():void
          > {
          > connection.call( "UserAcctService.getUsers",
          > new Responder( onResult_users, onFault ) );
          > }
          >
          > private function onResult_users( data:Object ):void
          > {
          > userCollection = new ArrayCollection( ArrayUtil.toArray( data ) );
          > }
          >
          > private function onFault( data:Object ):void
          > {
          > Alert.show( "Service Error" );
          > }
          >
          > private function saveUser():void
          > {
          > if ( selectedUser == null )
          > {
          > //Create new
          > var newC:UserAccount = new UserAccount();
          > newC.userID = 0;
          > newC.firstName = txt_fname.text;
          > newC.lastName = txt_lname.text;
          > newC.email = txt_email.text;
          >
          > connection.call( "UserAcctService.saveUser",
          > new Responder( onResult_save, onFault ), newC );
          > } else {
          > //Update old
          > var updateC:UserAccount = new UserAccount();
          > updateC.userID = selectedUser.userID;
          > updateC.firstName = txt_fname.text;
          > updateC.lastName = txt_lname.text;
          > updateC.email = txt_email.text;
          >
          >
          > connection.call( "UserAcctService.saveUser",
          > new Responder( onResult_update, onFault ), updateC );
          > }
          > }
          >
          > private function onResult_update( data:Object ):void
          > {
          > trace( data );
          > getUsers();
          > }
          >
          > private function onResult_save( data:Object ):void
          > {
          > trace( data );
          > getUsers();
          > }
          >
          > private function selectHandler( event:Event ):void
          > {
          > //PROBLEM SEEMS TO BE HERE; NOTHING
          > POPULATED
          > IN selectedUser
          > selectedUser = event.target.selectedItem as UserAccount;
          > }

          I'd put a break point in here and look to see if event.target.selectedItem
          is a UserAccount before you cast it. If not, there's a good chance your
          deserialization isn't working properly.
          http://flexdiary.blogspot.com/2008/11/thoughts-on-remoting.html

          HTH;

          Amy


          • 2. problem with my selectedItem
            hallik Level 1
            I guess it is not if I am reading the Eclipse debugger properly. When it breaks on the bolded line, this.selectedUser = null. Being so new to flex, I don't understand what you meant here in your blog post:

            "I think the real reason the serialization wasn't working was the last thing I discovered, which is that you have to have a dummy variable typed as your custom class in the same class as the RemoteObject that you're using to retrieve the values. This makes sure that the class definition gets compiled into the class where it can be used."

            A dummy variable in my mxml file? Could you please expand?

            I can see all the data I am trying to post in the data object also. So it's being returned, just not displayed in flex.
            <edit> I would also like to add that I am having a hard time reading through all the variables in the debug mode. There is just so much, my data is listed in tons of places, and I am having a hard time figuring out which one I am referring too, or if the right one is even there.

            for instance, there is nothing listed as target under the event "tree" in the variables view. I'll keep reading, but after all the docs I have read so far, my code looks right, but something isn't happening.

            Thanks,
            Nathan
            • 3. Re: problem with my selectedItem
              Level 7

              "hallik" <webforumsuser@macromedia.com> wrote in message
              news:gi3tc1$bpp$1@forums.macromedia.com...
              >I guess it is not if I am reading the Eclipse debugger properly. When it
              >breaks
              > on the bolded line, this.selectedUser = null. Being so new to flex, I
              > don't
              > understand what you meant here in your blog post:

              If you use the as keyword and the "thing" you are trying to cast is not of
              the right type, then you'll get null. Which you are. Look at the value in
              selectedItem, not selectedUser, which you already know is null. You need to
              figure out if it's null because selectedItem is null or because it's not an
              instance of your custom class.

              > "I think the real reason the serialization wasn't working was the last
              > thing I
              > discovered, which is that you have to have a dummy variable typed as your
              > custom class in the same class as the RemoteObject that you're using to
              > retrieve the values. This makes sure that the class definition gets
              > compiled
              > into the class where it can be used."
              >
              > A dummy variable in my mxml file? Could you please expand?

              I was using a separate class to communicate with the remoteObject, so it
              didn't really know or care what type of object it was supposed be
              retrieving. I had to make sure to add a variable that knew or the whole
              process failed. You already have a real variable that is typed to your
              class type, so you shouldn't need a dummy variable. I suspect the problem
              is with your alias.

              HTH;

              Amy


              • 4. Re: problem with my selectedItem
                hallik Level 1
                I am not exactly sure how I fixed it, but I changed the gateway.php file it was pointing to, and all of a sudden it started working. Thank you for your help!

                Now onto looping through the object and showing all the users permissions. :)

                Thanks again.
                • 5. Re: problem with my selectedItem
                  Level 7

                  "hallik" <webforumsuser@macromedia.com> wrote in message
                  news:gi475l$n5f$1@forums.macromedia.com...
                  >I am not exactly sure how I fixed it, but I changed the gateway.php file it
                  >was
                  > pointing to, and all of a sudden it started working. Thank you for your
                  > help!
                  >
                  > Now onto looping through the object and showing all the users permissions.
                  > :)

                  You're welcome :-). You may want to look at describeType and/or
                  describeTypeCache.