Archive for May, 2008

Tiny Flex/AS3 XML Settings Loader

May 19, 2008

I have been writing a lot of Flex code recently.
Somehow, I’m most pleased with this *tiny* little class to load XML settings from the HTML container.

The joy here is that Flash/Flex can *only* load data asynchronously… this class allows settings to be pulled in sychronously, before anything else, and with very little effort.


package com.wordpress.mapwrecker
{
	import flash.utils.Proxy;
	import flash.utils.flash_proxy;
	import flash.external.ExternalInterface;

	dynamic public class Settings extends Proxy {
		private var data:XML;

		public function Settings() {
			//load settings from parent HTML document
			if(ExternalInterface.available)
			{
				 var getSettingsRequest:String = "function(){ return document.getElementById(\"settings\").innerHTML; }";
				 var response:String = ExternalInterface.call(getSettingsRequest);
				 data = XML(response);
			}
		}

		override flash_proxy function getProperty(name:*):* {
			return data.children().(localName().toUpperCase() == String(name).toUpperCase());
		}
	}

}