Using an AS3 object or class as a DataProvider

I have a need to use custom class objects as a DataGrid dataprovider.  After wrestling with flash.utils.describeClass() I gave up and found two other methods for binding my custom objects to a DataGrid.

First here is a look at the sample MXML app I put together.

Using custom object as a DataGrid.dataProvider in Flex 3

Same object bound two different ways

Click here to run this demo in a new window.

In the first grid the DataGrid gets a column per object property.

var dp_horizontal:Array = ArrayUtil.toArray( myCustomObj );
grid_1.dataProvider = dp_horizontal.valueOf();

In the second grid, I run my custom object through a helper function to create an ArrayCollection containing NAME, VALUE pairs for each object property.

/**
 * Convert a class object into an ArrayCollection to be used
 * as a DataProvider
 */
private function objectToDataProvider( obj:Object ):ArrayCollection
{
	// our DataProvider
	var dp:ArrayCollection = new ArrayCollection();
	// get object class info
	var info:Object = mx.utils.ObjectUtil.getClassInfo( obj );
	// split the class property list into Array
	var propsA:Array = String( info.properties ).split(',');
	// loop over class properties grabbing value and filling up dp
	for( var xx:int = 0; xx <= propsA.length-1; xx++)
	{
		dp.addItem( { name : propsA[ xx ], value : obj[ propsA[ xx ] ] } );
	}

	return dp;
}

I’m currently using this helper function to bind the result of WebORB.NET data calls to a DataGrid for testing.  This code could really be used by any type of object though.

Hope this helps somebody else.

Advertisements

Keyboard doesn’t release a RadioButtonGroup’s focus

Today I was using Alert.show() while building a RadioButtonGroup change handler and noticed something interesting.

If you use your mouse to change the values of a RadioButtonGroup, Alert.show() will behave like a proper modal dialog.
However, if you use your keyboard’s arrow keys to toggle the values, the focus remains with your RadioButtonGroup.

The lesson here is since the focus remains with my RadioButtonGroup, you can continue to change the values all you want via keyboard.
In my situation, I kept receiving Alert.show()s, instead of one.

Try it for yourself.
First click on a RadioButton below, and notice that the Alert.show() keeps you from clicking on the RadioButtonGroup until you OK the prompt.

Now try the same thing again using your keyboard. If you do it enough times, the prompts will cover up the stage completely.

http://www.ericfickes.com/v1/code/flexRadioButtonAlertShow/alertblur.html


Get Adobe Flash Player