Verizon Wireless Media Store

The Verizon Wireless Media Store is another pretty impressive website built with Flex. It's nice to see more and more large corporations adopting the technology. In my eyes, the less I have to deal with JavaScript, CSS, and browser incompatibilities, the better. Bravo Verizon.

Connecting Abobe AIR (beta) to Coldfusion via mx:RemoteObject

While Flex 3 and AIR are still in beta, there are lots of little bugs and quirks that need to be worked around. Today I was porting over a Flex application to AIR, and received an error whenever using mx:RemoteObject. It looked something like the following:

[RPC Fault faultString="Send failed" faultCode="Client.Error.MessageSend" faultDetail="Channel.Connect.Failed error NetConnection.Call.Failed: HTTP: Failed: url: 'http://myApp-debug.swf/flex2gateway/'"]

I searched for hours and hours trying to find out why my application worked correctly as a regular Flex application but not as an AIR app. I finally stumbled upon the solution and I thought I would share... in order to save everyone else the same headache. The key is to explicitly specify the endpoint attribute in the RemoteObject tag. If you are testing locally it may look something like this:

<mx:RemoteObject id="AIR_Service"
     destination="ColdFusion"
     showBusyCursor="true"
     endpoint="http://localhost:8500/flex2gateway"
     source="myApp.cfc.cfcName">

This is something I hope Adobe will fix before the official release of both these applications, but in the meantime, here is your workaround.

Optional Attributes in ActionScript 3.0

You've gotta love ...rest! Since ActionScript 3.0 doesn't support overloaded constructors (booo), ...rest is a pseudo way of overloading your functions. ...rest is an argument parameter that allows you to pass in multiple arguments of various lengths and datatypes. So, for example, if you have a function that converts degrees Fahrenheit into degrees Celsius, you may need to call this function with various parameters:

//function to convert Fahrenheit to Celsius
private function toCelsius(_Fahrenheit:Object, ...rest):Number{
   var _Celsius:Number = (5/9)*(_Fahrenheit.degrees-32);
   return _Celsius
}

//passes single parameter, Fahrenheit object
Alert.show("Degress Celcius: " + toCelsius(degreeFahrenheit));

//passes two parameters, Fahrenheit object and length
degFarenheit = new DataGridColumn;
degFarenheit.labelFunction = toCelsius as Function;

By using ...rest, the toCelsius function will ignore any additional attributes that might get passed in and that would normal throw an error. What's even better, is that you can name your ...(rest) parameter and ActionScript will store a reference to all the parameters passed in as an array. This is what really allows you to create one method signature that can act like overloaded functions. For example:

//you can name your optional parameter and reference it accordingly
private function overloadTest(...myArguments):String{
switch (myArguments.length){
case 0:
... code A ...
return "signature A";
case 1:
... code B ...
return "signature B";
case 2:
... code C ...
return "signature C";
default:
... code D ...
return "signature D";
}
}

And although this doesn't make up for ActionScript's lack of overloading support, it does make life a little simpler and more flexible. I am a big fan of ...rest!

Dynamic Identifiers In ActionScript 3.0

I came across a problem in Flex the other day where I was trying to dynamically identify an image with ActionScript. I'll spare you the programmatic details, all you need to know is that Coldfusion returns a string that corresponds to the name of an image inside my Flex application. The image name is then used to modify the image object with the corresponding ID.

Now, I know I could have accomplished my goal by using a Switch statement but the number of images in the application would have made the code tedious to write and hard to maintain. So after much searching I found how to dynamically identify objects in my flex application.

Here is an example of how to change the source of an image, where the image's ID is produced dynamically.

SAMPLE CODE:
// if the object is in the same component, use THIS scope
this[imageName].source = "images/newImage.jpg";
// or if the object is part of a sub-component, use the component's ID as the scope
subComponentID[imageName].source = "images/newImage.jpg";

I hope this simple example is enough to clarify how to dynamically refer to an ID of an object in ActionScript 3.0; I did a lot of googling before I figured out the syntax for this - even though it's rather simple. So I hope this is enough to save some of you a bit of searching.

BlogCFC was created by Raymond Camden. This blog is running version 5.7.002.