Flex Pasta » Flex Utils
So you’re coding up your flex app and you want to pass a url parameter into your application. Like getting the color parameter from mydomain.com?color=red. While one option is to call the built in flex option of Application.application.parameters, this will only work if your example points directly to the swf. If your url is main.swf?color=red then Application.application.parameters.color will work fine for you. However, if like most people, have the swf wrapped in an html page(main.html?color=red), this approach won’t work. I ran into this problem myself, and I didn’t want to get into the messy business of editing the flash vars, especially if I had a large number of parameters or wanted to add more on the fly later. I created a HTTPUtil class in actionscript to get the url information. The class uses javascript to get all of the url information, including host name, port, and url parameters among other things. The most helpful method you will find is getParameterValue(key:String):String which will give you back the parameter value for the given key.
Have multiple environments and need to get the full url with the host name and port in actionscript?
There are several methods to help with this:
getHostName()
getPort()
getProtocol() -such as http or https
getContext() -The path after the hostname but before the url parameters
Attached is the source. You can see that the methods are all static and use javascript to get the information back to flex. It is designed to get this information regardless of where the flex swf file lives since most people have them embeded in an html file.
Simple string manipulation is done on a daily basis in any language including flex. By using utility classes, you can reduce simple code errors and increase readability. I find the mx.utils.StringUtil could use a little more muscle than the 4 methods given out of the box. Downloadable is my own StringUtil class that beefs up the support for common string functions. The additions are based on the methods found in Apache Commons Lang open source code that includes a popular org.apache.commons.lang.StringUtils class.
Here is a summary of some of the methods you will find:
isEmpty : Checks if a String is empty (”") or null.
isNotEmpty : Checks if a String is empty (”") or null.
isBlank : Checks if a String is whitespace, empty (”") or null.
isNotBlank : Checks if a String is not whitespace, empty (”") or null.
defaultIfEmpty : Returns either the passed in String, or if the String is empty or null, the value of defaultStr.
If you have a String Utility method for flex/actionscript, post it!