Binding XML as dataProvider in Flex 4

Problem: You want to bind an XMLList object (such as the results you get from a static or dynamically generated XML file) as the data source for some views (called data provider components) in your UI. You assign the XMLList to the dataProvider property like this:

<s:DataGroup dataProvider="{menuLoader.lastResult.step[0].item}"
itemRenderer="components.MMChoicesItemRenderer">...</s:DataGroup>

<s:HTTPService id="menuLoader"
resultFormat="e4x"
url="mymenu.xml"
fault="onMenuHTTPFault(event)"
result="onMenuHTTPResult(event);"/>

But you get a runtime TypeError,  something like:

TypeError: Error #1034: Type Coercion failed: cannot convert XMLList@284a62a9 to mx.collections.IList.

Reason:

Despite the fact that it’s named “List”, XMLList turned out to be a top level Flash object that doesn’t implement the mx.collection.IList interface, which is required for a data source to be qualified as a DataProvider. The commonly used classes that implement IList are ArrayCollection, ArrayList (curiously) and XMLListCollection.

Solution:

The TypeError happens when you tried to set the resultFormat of your HTTPService to “e4x” and bind it directly to the dataProvider property of some components.

Option 1 is to wrap the XMLList into an XMLListCollection before using it as a dataProvider. Like so:

<s:DataGroup dataProvider="{new XMLListCollection(XMLList(menuLoader.lastResult.step[0].item))}" >...</s:DataGroup>

Option 2 is to not use “e4x” as the resultFormat, instead use the default “object” as the resultFormat, then you are free to bind the result directly as dataProvier:

<s:DataGroup dataProvider="{menuLoader.lastResult.steps.step[0].item}" >...</s:DataGroup>

Notice that when the resultFormat is not set to e4x, the HTTPService.lastResult is not the top XML object any longer and you need to drill down one more level to get the XML object.

Also an example from Flex Examples.

Advertisements

4 Responses to “Binding XML as dataProvider in Flex 4”

  1. Craig Mc Says:

    I cant seem to get this to work for any reason.

    Here is what I got:

    FACTORY STATUS
    file:/D:/Work/RapidReport/Images/Charts/Vertical-Linear-Gauges.png
    1

    My item renderer then does this:

    I would aprieciate any help you can give on this, I am very stuck.

  2. Craig Mc Says:

    Hmm I guess word press does not like any kind of markup.

  3. Daniel Says:

    Thanks for the info!

  4. Avneesh Pandey Says:

    Really Nice One

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: