February 21st, 2007 by Kyle
Tags: ActionScript, Flex, mxml
Posted in: ActionScript, Flex
[Bindable] actually generates getter/setters, which then show up as accessors during introspection. This actually confused myself and a customer for a little while until I realized that of course, the binding generates getters and setters which means that what where once normal properties now are more than just properties. Run the code with the [Bindable] tag commented and uncommented to see the difference.
MyClass.as:
import mx.collections.ArrayCollection;
// [Bindable]
public class MyClass {
public var someNumber:Number;
public var someString:String;
public var someInt:int;
private var _somePrivateString:String;
public function MyClass()
{
this.someNumber = -1;
this.someString = ”;
this.someInt = 1;
}
public function get somePrivateString():String{
return _somePrivateString;
}
public function set somePrivateString(s:String):void{
_somePrivateString=s;
}
}
}
main.mxml:
<!– usingas/IntrospectionAPI.mxml –>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns="*" creationComplete="getDetails()">
<mx:Script><![CDATA[
public function getDetails():void {
// Get the Button control’s E4X XML object description.
var mcl:MyClass=new MyClass()
var classInfo:XML = describeType(mcl);
// Dump the entire E4X XML object into ta2.
ta2.text = classInfo.toString();
// List the class name.
ta1.text = "Class " + classInfo.@name.toString() + "\n";
// List the object’s variables, their values, and their types.
for each (var v:XML in classInfo..variable) {
ta1.text += "Variable " + v.@name + "=" + mcl[v.@name] +
" (" + v.@type + ")\n";
}
// List accessors as properties.
for each (var a:XML in classInfo..accessor) {
// Do not get the property value if it is write only.
if (a.@access == ‘writeonly’) {
ta1.text += "Property " + a.@name + " (" + a.@type +")\n";
}
else {
ta1.text += "Property " + a.@name + "=" +
mcl[a.@name] + " (" + a.@type +")\n";
}
}
// List the object’s methods.
for each (var m:XML in classInfo..method) {
ta1.text += "Method " + m.@name + "():" + m.@returnType + "\n";
}
}
]]></mx:Script>
<mx:Label text="Enumeration of MyClass.as’s properties, accessors and methods."/>
<mx:TextArea id="ta1" width="400" height="200"/>
<mx:Label text="E4X XML object description of MyClass.as"/>
<mx:TextArea id="ta2" width="400" height="600"/>
</mx:Application>










