Here’s a class that comes in handy from time to time. It allows you to store key/value pairs, for configuration purposes for instance, and load them from an external XML source. You can also modify the settings by code. Here are some examples:
To add a setting or change the value of an existing setting, simply define it as a property as follows:
AppSettings.getInstance().mySetting = “myValue”;
To get a setting, request it as a property:
var result:String = AppSettings.getInstance().mySetting;
To delete a setting, use the delete operator:
delete AppSettings.getInstance().mySetting;
The XML format is similar to that of .Net’s AppSettings config section. All settings are contained in a
Example:
<appsettings>
<add key=”myFirstSetting” value=”aValue”/>
<add key=”mySecondSetting” value=”anotherValue”/>
</appsettings>
To load these XML settings, just call the load() method:
AppSettings.getInstance().load(”pathtosettings.xml”); (to load from an xml file)
or
AppSettings.getInstance().load(myXmlObject); (to load from an xml object)
To use this class, just place the herrodius.swc from the bin folder in your classpath. More info is available in the release notes.
This class uses the flash_proxy class to control the dynamic properties. It is similar to the older __resolve() method but offers much better control.
The download contains the binaries, the docs and the source files: herrodius-as3lib-v0.1.zip
Any feedback is greatly appreciated.
Add to Bloglines - Digg This! - del.icio.us - Stumble It! - Twit This! - Technorati links - Share on Facebook - Feedburner
Christophe Herreman is a software developer living in Belgium. He's working on high-end Flex and AIR solutions at
March 17th, 2007 at 6:46 pm
Looks brilliant, I’ll try it and get back to you on how I get on. It’s being used here: http://www.aireofs.com
January 11th, 2008 at 4:39 pm
Really good work! I have one question though. When using this class in a Application, one will typically want the xml file to be fully loaded before any call to getProperty is made, but, as the XML load process is asynchronous, how can one synchronize the XML complete load to the application initialization?
January 11th, 2008 at 5:01 pm
Hi Gabriel,
you would have to load the xml in a creationComplete handler and then init your application when the xml is loaded. You can also add bindings to the values in the AppSettings class. e.g. <mx:Button label=”{AppSettings.getInstance().myButtonLabel}”/>. That way you just have to load the xml and need not to worry about initializing your application by reading the settings and applying them.