Using XML and ActionScript with Flex Applications - Getting the Class by Name
(Page 5 of 5 )
If you have a class name, you can retrieve a reference to the class using the getDefinitionByName() function. The function requires a string parameter specifying a class name, and it returns an Object type. The function returns an Object type rather than aClasstype because it could also theoretically return a reference to a function if you pass it a fully qualified function name (e.g.,flash.util.getTimer). If you’re certain that you’re retrieving a class reference, you can cast the return value toClass, as in the following example:
var classReference:Class = Class(getDefinitionByName("flash.net.URLLoader"));
Once you’ve retrieved a reference to a class, you can create anewinstance, as follows:
var instance:Object = new classReference();
Obviously you can use the return value fromgetQualifiedClassName()orgetQualifiedSuperclassName()in conjunction withgetDefinitionByName(), as in the following example:
var loader:URLLoader = new URLLoader();
var className:String = getQualifiedClassName(loader);
var classReference:Class = Class(getDefinitionByName(className));
var instance:Object = new classReference();
Class Introspection
You can use describeType() to return a description of all the events, public properties, and public methods of an object. Simply pass the method a reference to the object you want to introspect. The method returns an XML object that details the class name, superclass, various class settings, implemented interfaces, constructor signature, public method signatures, and public properties descriptions.
The following example retrieves the description for aURLLoaderobject:
var loader:URLLoader = new URLLoader();
var description:XML = describeType(loader);
trace(description);
The preceding example outputs the following:
<type name="flash.net::URLLoader" base="flash.events::EventDispatcher"
isDynamic="false" isFinal="false" isStatic="false">
<metadata name="Event">
<arg key="name" value="httpStatus"/>
<arg key="type" value="flash.events.HTTPStatusEvent"/>
</metadata>
<metadata name="Event">
<arg key="name" value="securityError"/>
<arg key="type" value="flash.events.SecurityErrorEvent"/>
</metadata>
<metadata name="Event">
<arg key="name" value="ioError"/>
<arg key="type" value="flash.events.IOErrorEvent"/>
</metadata>
<metadata name="Event">
<arg key="name" value="progress"/>
<arg key="type" value="flash.events.ProgressEvent"/>
</metadata>
<metadata name="Event">
<arg key="name" value="complete"/>
<arg key="type" value="flash.events.Event"/>
</metadata>
<metadata name="Event">
<arg key="name" value="open"/>
<arg key="type" value="flash.events.Event"/>
</metadata>
<extendsClass type="flash.events::EventDispatcher"/>
<extendsClass type="Object"/>
<implementsInterface type="flash.events::IEventDispatcher"/>
<constructor>
<parameter index="1" type="flash.net::URLRequest" optional="true"/>
</constructor>
<variable name="bytesTotal" type="uint"/>
<variable name="data" type="*"/>
<method name="load" declaredBy="flash.net::URLLoader" returnType="void">
<parameter index="1" type="flash.net::URLRequest" optional="false"/>
</method>
<method name="close" declaredBy="flash.net::URLLoader" returnType="void"/>
<variable name="dataFormat" type="String"/>
<variable name="bytesLoaded" type="uint"/>
<method name="dispatchEvent" declaredBy="flash.events::EventDispatcher"
returnType="Boolean">
<parameter index="1" type="flash.events::Event" optional="false"/>
</method>
<method name="toString" declaredBy="flash.events::EventDispatcher"
returnType="String"/>
<method name="willTrigger" declaredBy="flash.events::EventDispatcher"
returnType="Boolean">
<parameter index="1" type="String" optional="false"/>
</method>
<method name="addEventListener" declaredBy="flash.events::EventDispatcher"
returnType="void">
<parameter index="1" type="String" optional="false"/>
<parameter index="2" type="Function" optional="false"/>
<parameter index="3" type="Boolean" optional="true"/>
<parameter index="4" type="int" optional="true"/>
<parameter index="5" type="Boolean" optional="true"/>
</method>
<method name="hasEventListener" declaredBy="flash.events::EventDispatcher"
returnType="Boolean">
<parameter index="1" type="String" optional="false"/>
</method>
<method name="removeEventListener" declaredBy="flash.events::EventDispatcher"
returnType="void">
<parameter index="1" type="String" optional="false"/>
<parameter index="2" type="Function" optional="false"/>
<parameter index="3" type="Boolean" optional="true"/>
</method>
</type>
With some work, you can create complex systems that use objects to create sophisticated and dynamic applications.
Summary
In this chapter, we discussed the fundamentals of ActionScript 3.0. ActionScript is the ECMAScript-standard-based programming language used by Flex applications. Although the topic of ActionScript is far too complex to discuss comprehensively in one chapter, we have covered many of the basics you’ll need to get started writing ActionScript code, including where to place the code, basic syntax, common data types, how to write classes, the event model, error handling, working with XML, and reflection.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
|
This article is excerpted from chapter four of the book Programming Flex 2, written by Chafic Kazoun and Joey Lott (O'Reilly, 2007; ISBN: 059652689X). Check it out today at your favorite bookstore. Buy this book now.
|
|