2 Replies Latest reply on Aug 24, 2007 2:00 PM by rbetieh

    Newby Language Question


      I am still just beggining to learn flex so Im sure this is an easy one, but I havnet "gotten it" yet. Hopefully you guys can point me in the write direction.

      I have an XML that I am using as a datamodel. It looks something like this:


      For right now im using an Mx:Model to bring this data into the program. I have an array collection declared as:

      baseArrCol = new ArrayCollection(modelname.item);

      At some point I need to do some calculations for an item based on its sub-array. I figured I could just create another array collection something like this

      subArrCol = new ArrayCollection( (baseArrCol.getItemAt(IndexImWorkingWith)).itemArray1.ArrayItem);

      This works when the sub-array has more than 1 item, but if there is only 1 item I get the following error:

      TypeError: Error #1034: Type Coercion failed: cannot convert mx.utils::ObjectProxy@439c821 to Array.

      So when I have a sub-array of exactly 1 item the ArrayCollection did not treat the field as an array, so I fail to get the data in this situation.

      Im probably doing this the wrong way, is there a better way to do this?

        • 1. Re: Newby Language Question

          One thing to bear in mind is that although your data starts as XML, when you bring it in to your program using <mx:Model> it is being turned into a tree of untyped ActionScript objects. Even if you're using an external source with Model, it is being pulled in at compile time rather than runtime. This is being done in a way that is ignorant of any XML schema in use; it uses a very straightforward method of constructing the object tree. The way it constructs the tree, multiple children with the same name become an array but a single child just becomes an object (or rather an ObjectProxy, so you can listen for changes & use binding).

          You can do more or less what you're already doing, but you have to be sensitive to this...check types. You might also skip creating an ArrayCollection if you're just using it to access the data. So, something like this:

          var itemArray:Object = items.item[index].itemArray1;
          if (itemArray is Array)

          Note that you'll have the same problem if you have only one item (the array index will fail).

          A bit of a nuisance, but it will work.

          Alternatively, you could include the data as XML, using the <mx:XML> tag. Then you'd work with the data using XML methods. (Even there you have a choice between E4X format and legacy format).

          In the absence of more details, my personal preference would probably be to use <mx:XML> with E4X, which is fairly powerful, but the E4X documentation isn't as strong as it could be. But different scenarios might make one approach better than the other.

          Mike Schiff
          Flex SDK team.
          • 2. Re: Newby Language Question
            rbetieh Level 1
            Hi Mike,

            Thanks for the tip, I used your first suggestion (checking for array-ness) as a first step to getting my test code to work.

            Now that it works, I have been looking into XML datatypes and was interested in knowing if there are any books out there that deal with datatype philosophy for the flex language. For instance, which collection class is better suited for which container class and why. It looks to me like XML and tree fit hand in hand.

            I tried to apply XMLCollection to the AdvancedDataGrid with some rather interesting results. Heres what I did:

            1) Changed the Mx:Model to Mx:XML (source being an external XML)
            2) Pushed XMLModel.item into an XMLCollection that replaces the ArrayCollectionItem
            3) Created an AdvancedDataGrid with the XMLCollection as the dataProvider and a column for itemfield1

            So far so good, everything is working as expected. Now I replace the dataProvider with a groupingCollection dataProvider. I use itemfield1 as the groupingField.

            Interestingly enough when I run the new code, the grouped column behaves more like a tree. The base is the grouping field I chose, but there are implicit sub-grouppings. In other words, after I open folder "a" I see a folder for ArrayItem and opening that shows a folder for ArrayItemField and opening that reveals the data in ArrayItemField. The results are to me, rather unexpected, but still rather amazing at the same time. I wonder if there is a good way to use this, perhaps a sortable tree?