3 Replies Latest reply on May 30, 2008 4:37 PM by fneslon

    Some Basic Data Binding Problems

    fneslon
      Hi. I'm using Flex Builder 3 and I'm trying to bind some remote data to a combobox. I think I'm missing something very fundamental in Flex Builder debugging, because I just can't find anything to help me through this. But here are some examples:

      #1 Does Not Work (I get an empty combo box, but it is sized right, like it's fitting to content, but empty)
      This closely resembles an example the Flex Builder Help
      ------------------------------------------------------------------------------------------ ----------------------------------------------------
      <mx:ArrayCollection id="usersList" source="{mx.utils.ArrayUtil.toArray(srv.getUsers().lastResult)}"/>
      <mx:ComboBox id="UserBox" dataProvider="{usersList}" labelField="name"></mx:ComboBox>

      #2 Same as above (only difference in code is that I removed "()" from the method call), same result
      ------------------------------------------------------------------------------------------ ----------------------------------------------------
      <mx:ArrayCollection id="usersList" source="{mx.utils.ArrayUtil.toArray(srv.getUsers.lastResult)}"/>
      <mx:ComboBox id="UserBox" dataProvider="{usersList}" labelField="name"></mx:ComboBox>


      #3 Same results as above
      ------------------------------------------------------------------------------------------ ----------------------------------------------------
      <mx:ArrayCollection id="usersList" source="{srv.getUsers.lastResult}"/>
      <mx:ComboBox id="UserBox" dataProvider="{usersList}" labelField="name"></mx:ComboBox>


      #4 Does not work, I just get a small, empty combobox (not sized to would-be content)
      ------------------------------------------------------------------------------------------ ----------------------------------------------------
      <mx:ComboBox id="UserBox" dataProvider="{srv.getUsers.lastResult}" labelField="name"></mx:ComboBox>


      #5 This is the only thing that works. And I only figured it out through countless
      trial & error iterations.
      ------------------------------------------------------------------------------------------ ----------------------------------------------------
      <mx:ArrayCollection id="usersList" source="{mx.utils.ArrayUtil.toArray(srv.getUsers.lastResult)}"/>
      <mx:ComboBox id="UserBox" dataProvider="{srv.getUsers.lastResult}" labelField="name"></mx:ComboBox>


      So, am I losing my mind or what? Why on earth would the last one work? The 2nd element is not even referring to the 1st element, so why is it dependent on it to work?
      I know I'm giving limited information and so it might be difficult anyone to give me a direct answer. But I can't even figure out how to debug this stuff. Like, I can't even figure out how to determine what TYPE of object is being returned by 'lastResult'. I've tried setting stop points, etc. but no help. None of the examples I give above bomb with any kind of error. It just pretends like everyhting is fine, leaving me without a clue as to what the problem is.
      I feel like i'm trying to program blind. I'm new to Flex as I'm sure you can tell. In the future when I face these kind of things any good debugging tips?

      Pulling my hair out. Anybody have any ideas? Pretty please?


        • 1. Re: Some Basic Data Binding Problems
          fneslon Level 1
          Ok, let me try this another way. Could somebody please point me to a simple, working example of binding a combobox to a collection retrieved form a remote service (mx:RemoteObject)? The Help samples are not working for me. But they only give only snippets code. I think I need to see the RemoteObject declaration, the ArrayCollection and ComboBox elemetns (and anything else that's required) because I'm obviously missing something. Using scripting, I'm able to grab the collection from the remote service and inspect them, so that part is working fine.
          Anybody know where there's a working sample with source code?
          <mx:RemoteObject xxxxxxxxxx/>
          <mx:ArrayCollection xxxxxxxxx/>
          <mx:ComboBox xxxxxxxx/>
          ... etc.

          thanks
          • 2. Re: Some Basic Data Binding Problems
            ntsiii Level 3
            First, all data calls in Flex are asynchronous. You can never access the result data in the same function that you call for it. So this will never work:
            mx.utils.ArrayUtil.toArray(srv.getUsers().lastResult)

            The last example works because the first declaration invokes the method call. That code does NOT assign any value to UsersList (rather it assigns null) because the call is async. The second declaration in the working example binds to lastResult. So when the async method call returns, the binding places the data into the Combo box.

            Second, do not bind directly to lastResult. It is too limiting and is very HARD TO DEBUG. Instead Use a result handler.

            1) invoke the method call: srv.getUsers.send(); Do this in an init function called by the app creationComplete event

            2) declare a result handler on your RemoteObject or on the method:
            result="onResult(event)"

            3) declare a var to hald the data and declare the result handler:
            import mx.rpc.events.ResultEvent; //from memory
            private var _acUsersList:ArrayCollection;
            private function onResult(event:ResultEvent):void {
            var oResult:Object = event.result;
            trace(ObjUtil.toString(oResult)); //from memory. to see the data in the result. I prefer to use xml here
            var aUsers:Array = oResult.users; //this depends on your data structure.
            _acUsersList = new ArrayCollection(aUsers); //create ac and assign to var
            trace(_acUsersList.length); //to verify yo have your data
            4) Bind to the variable:
            <mx:ComboBox id="UserBox" dataProvider="{_acUsersList}"

            Try this and post back.

            Tracy
            • 3. Re: Some Basic Data Binding Problems
              fneslon Level 1
              Excellent. Thank you so much. Your example works. I'm still not sure why the Help document puts me on the wrong path, unless I'm just not reading it right. Anyway, I had tried using the event handler, but the part I was missing, was assigning the result data to the instance-level variable. That did the trick. Also, thanks for the tip on avoiding binding directly to lastResult for debugging purposes. That's a big one.