Monday, July 23, 2007

About data providers and collections

The simplest data provider can be an array of strings or objects; for example, you can use the following code to define a static ComboBox control:

<?xml version="1.0"?>

<
mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<
mx:Script>
<![
CDATA[
[
Bindable]

public var
myArray:Array = ["AL", "AK", "AR"];
]]>

</
mx:Script>
<
mx:ComboBox id="myCB0" dataProvider="{myArray}"/>

</
mx:Application>


However, using a raw data object, such as an Array or Object, as a data provider has limitations:

Raw objects are often not sufficient if you have data that changes, because the control does not receive a notification of any changes to the base object. The control, therefore, does not get updated until it must be redrawn due to other changes in the application or if the data provider is reassigned; at this time, it gets the data again from the updated Array. In the preceding example, if you programmatically add states to the Array, they do not show up in the ComboBox control unless you reassign the control's dataProvider property.
Raw objects do not provide advanced tools for accessing, sorting, or filtering data. For example, if you use an Array as the data provider, you must use the native Adobe Flash Array methods to manipulate the data.
Flex provides a collection mechanism to ensure data synchronization and provide both simpler and more sophisticated data access and manipulation tools. Collections can also provide a consistent interface for accessing and managing data of different types. For more information on collections, see About collections.

Data provider types
The Flex framework supports two basic types of data provider:

Linear or list-based data providers are flat data consisting of some number of objects, each of which has the same structure; they are often one-dimensional Arrays, or objects derived from such Arrays, such as ActionScript object graphs. (You can also use an XMLListCollection object as a linear data provider.)

You can use list-based data providers with all data provider controls, and typically use them for all such controls except Tree and most menu-based control instances. If the data provider contents can change dynamically, you typically use the ArrayCollection class and either the IList or ICollectionView interface methods and properties to represent and manipulate these data providers.

Hierarchical data providers consist of cascading levels of often asymmetrical data. Often, the source of the data is an XML object, but the source can be a generic Object or concrete class. You typically use a hierarchical data providers with Flex controls that are designed to display hierarchical data:

Tree
Menu, MenuBar, and PopUpMenuButton.
You can also extract data from hierarchical data provider to use in controls such as List or DataGrid that take linear data.

A hierarchical data provider defines a data structure that matches the layout of a tree or cascading menu. For example, a tree often has a root node, with one or more branch or leaf child nodes. Each branch node can hold additional child branch or leaf nodes, but a leaf node is an endpoint of the tree.

The Flex hierarchical data controls use data descriptor interfaces to access and manipulate hierarchical data providers, and the Flex framework provides one class, the DefaultDataDescriptor class that implements the required interfaces. If your data provider does not conform to structure required by the default data descriptor, you can create your own data descriptor class that implements the required interfaces.

You can use an ArrayCollection class, XMLListCollection class, ICollectionView interface or IList interface to access and manipulate dynamic hierarchical data.

No comments: