Flex: Method Invocation By Name

Say you’re done introspecting and you want to invoke a method of the object you introspected. Simply use the ff:

var methodName:String = “doSomething”;
var method:Function = object[methodName] as Function;
var returnValue:* = method.apply(object, [“string1”, “string2”]);  // or method.call(“string1”, “string2”);

Once caveat, if you’re a Java programmer, you would expect getters and setters to be methods. But in Flex, they’re actually accessors and the above method won’t work. Instead, directly access the attribute by name:

var attributeName:String = “doSomething?;
var returnValue:* = object[attributeName];

Flex: Introspecting An Object By Name

Say you have an object reference in your class:

var button1:Button;

How do you introspect it? One way is to use the Introspection API as described in the Flex 3: Performing Object Introspection doc:

var classInfo:XML = describeType(button1);

But what if you want to use the object name, a string, instead of a class reference? Simply use the ff:

var objectName:String = “button1”;
var objectInfo:XML = describeType(this[objectName]);

From there it’s as simple as going through the XML object as described the rest of the Flex 3 doc.

Flex: Non-Breaking Space

We encountered some text that was breaking at the wrong place in our Flex application. Using   didn’t work at all. After a bit of puzzling and searching, we found the answer. Instead of   we should use  . It worked!

Flex: Security Error Accessing URL Part 2

I previously encountered this error when I was working only within my own development box (laptop actually) and I basically worked around the error. But now, I need my Flex Web Service client separate from my Web Service. when you are in this situation, you need to have a crossdomain.xml file in your host’s root directory:

<cross-domain-policy>
<site-control permitted-cross-domain-policies=”master-only”/>
<allow-access-from domain=”yourdomain”/>
<allow-http-request-headers-from domain=”yourdomain” headers=”*”/>
</cross-domain-policy>

And if the host is a secure/HTTPS server, you just need to add the secure attribute:

<cross-domain-policy>
<site-control permitted-cross-domain-policies=”master-only”/>
<allow-access-from domain=”yourdomain” secure=”false”/>
<allow-http-request-headers-from domain=”yourdomain” headers=”*” secure=”false”/>
</cross-domain-policy>

Flex: Custom HTML Wrappers In Ant

Building Flex using ant build files is generally straightforward. Unfortunately, this is not so with using custom HTML wrappers. You cannot use the html-wrapper task since this only uses the standard templates in Flex’s html-template folder. If you want to use your own index.template.html in your own html-template folder, you will need to use a workaround:

<macrodef name=”generateHtmlWrapper” description=”Generates HTML Wrapper using custom template”>
<attribute name=”file”/>
<attribute name=”title”/>
<attribute name=”application”/>
<attribute name=”swf”/>
<attribute name=”width”/>
<attribute name=”height”/>
<attribute name=”bgcolor”/>
<attribute name=”version-major”/>
<attribute name=”version-minor”/>
<attribute name=”version-revision”/><attribute name=”template”/> <attribute name=”output”/>

<sequential>
<copy todir=”@{output}/history”>
<fileset dir=”html-template/history”/>
</copy>             
<copy file=”html-template/AC_OETags.js” todir=”@{output}”/>
<copy file=”html-template/playerProductInstall.swf” todir=”@{output}” />
<copy file=”html-template/index.template.html” tofile=”@{output}/@{file}” />

<replace file=”@{output}/@{file}” token=”$${title}” value=”@{title}”/>
<replace file=”@{output}/@{file}” token=”$${swf}” value=”@{swf}”/>
<replace file=”@{output}/@{file}” token=”$${width}” value=”@{width}”/>
<replace file=”@{output}/@{file}” token=”$${height}” value=”@{height}”/>
<replace file=”@{output}/@{file}” token=”$${bgcolor}” value=”@{bgcolor}”/>
<replace file=”@{output}/@{file}” token=”$${application}” value=”@{application}”/>
<replace file=”@{output}/@{file}” token=”$${version_major}” value=”@{version-major}”/>
<replace file=”@{output}/@{file}” token=”$${version_minor}” value=”@{version-minor}”/>
<replace file=”@{output}/@{file}” token=”$${version_revision}” value=”@{version-revision}”/>
</sequential>
</macrodef>

Thanks to Renaun Erickson for the solution!