4 Replies Latest reply on Oct 7, 2008 8:30 PM by jonesf

    Binding XML to a List

    jonesf Level 1
      Hi,
      I'm surely missing something obvious here. Let's say I have an XML object where root node is "dealership", then below that is a "cars" node, which will ultimately contain a list of "car" nodes (but is empty to start). When the page initiates, there are no "car" nodes. I have a listbox, where the user drags and drops "car" objects into. I want this to populate my "cars" node with "car" nodes (one for each item in the list). Everything is fine except for the binding. I'll spare you all of the iterations I've gone through of things I've tried.. could someone tell me what's the right way to do this? My binding just doesn't take. My list will have items, but they are never added to my dataProvider xml.
      See pseudo-code below...

      Thanks!
        • 1. Re: Binding XML to a List
          Level 7

          "jonesf" <webforumsuser@macromedia.com> wrote in message
          news:gcf0nb$fuj$1@forums.macromedia.com...
          > Hi,
          > I'm surely missing something obvious here. Let's say I have an XML object
          > where root node is "dealership", then below that is a "cars" node, which
          > will
          > ultimately contain a list of "car" nodes (but is empty to start). When
          > the
          > page initiates, there are no "car" nodes. I have a listbox, where the
          > user
          > drags and drops "car" objects into. I want this to populate my "cars"
          > node
          > with "car" nodes (one for each item in the list). Everything is fine
          > except
          > for the binding. I'll spare you all of the iterations I've gone through
          > of
          > things I've tried.. could someone tell me what's the right way to do this?
          > My
          > binding just doesn't take. My list will have items, but they are never
          > added
          > to my dataProvider xml.
          > See pseudo-code below...
          >
          > Thanks!
          >
          >
          > var xml:XML = <dealership><cars/></dealership>
          > var list:List = new List();
          > list.dataProvider = xml.cars.car;
          >
          > ------
          > after adding xml items to the list, I want 'xml' to look something like
          > this
          > (but it never happens
          > -------
          >
          > <dealership>
          > <cars>
          > <car type="honda"/>
          > <car type="ford"/>
          > <car type="toyota"/>
          > </cars>
          > </dealership>

          What is the source of the XML items you want to add to the list? Are they
          coming from a different XML object, or are you making them out of thin air?


          • 2. Re: Binding XML to a List
            jonesf Level 1
            Out of thin air.

            Something like...
            • 3. Re: Binding XML to a List
              Level 7

              "jonesf" <webforumsuser@macromedia.com> wrote in message
              news:gcfqur$j80$1@forums.macromedia.com...
              > Out of thin air.
              >
              > Something like...
              >
              > var child:XML = <car/>;
              > child.@type= "honda";
              > child.@label= "Accord";

              Something like

              private var dp:XMLListCollection;

              private fynction init():void{
              var sourceXML:XML=<dealership><cars/></dealership>;
              var targetNode:XML=sourceXML.cars[0];
              var childXML:XML= <car type="Honda" label="Accord"/>
              targetNode.appendChild(childXML);
              dp = XMLListCollection(sourceXML.child('cars').child('car'));
              }

              then bind to dp.

              This was off the top of my head. If you get any syntax errors, try
              http://dispatchevent.org/roger/as3-e4x-rundown/ for more information.

              HTH;

              Amy


              • 4. Re: Binding XML to a List
                jonesf Level 1
                Hi Amy,
                Thank you so much for you help. I also wasn't familiar with this notation..
                sourceXML.child('cars').child('car')
                So, that was a big help.

                After much hair pulling, I finally discovered my problem. Once I had finally figured out the problem, I sat down and came up with a fully coded test case that I could post here to ask if it was a bug or a 'feature'. Now, knowing what the problem was, I was able to google for it more precisely, and finally found some discussion on it (looks to be a bug).

                So what was the problem? Short version of the story, is that having dragMoveEnabled=true on the source listbox doesn't bind correctly when dragging from it and dropping into another listbox that's bound to an xml list. If I set dragMoveEnabled=false, then the binding works fine (but unfortunately, now the drag/drop copy behavior is not what I want). At least I have something to work with now.

                Some more discussion on it at the following link.
                http://www.mail-archive.com/flexcoders@yahoogroups.com/msg59889.html