Skip to content

Instantly share code, notes, and snippets.

@creynders
Created August 9, 2011 11:45
Show Gist options
  • Save creynders/1133833 to your computer and use it in GitHub Desktop.
Save creynders/1133833 to your computer and use it in GitHub Desktop.
Use of parsers
//wherever you do your mapping
injector.mapSingletonOf( IConfigDataParser, XMLConfigDataParser )
//IConfigDataParser
function parseData( rawData : String ) : ConfigDataVO
//XMLConfigDataParser implements IConfigDataParser
public function parseConfigData( rawData : String ) : ConfigDataVO{
var output : ConfigDataVO = new ConfigDataVO();
var xmlData : XML = new XML( rawData );
//-- parse the xml data as it needs to be done, for example
output.logoURL = xmlData..logoURL
//--
return output
}
//IAppConfigRetrieverService
function get configDataLoadedSignal() : ConfigDataLoadedSignal;
function loadConfigData():void;
//AppConfigRetrieverService implements IAppConfigRetrieverService
[Inject]
public var parser : IConfigDataParser;
private var _configDataLoadedSignal : ConfigDataLoadedSignal
public function get configDataLoadedSignal() : ConfigDataLoadedSignal{
return _configDataLoadedSignal;
}
[Inject]
public function set configDataLoadedSignal( value : ConfigDataLoadedSignal ) : void{
_configDataLoadedSignal = value;
}
private var _loader : URLLoader;
public function loadConfigData():void{
//load config data
}
private function onConfigDataLoadedEvent( event : Event ) : void{
var rawConfigData : String = _loader.data as String
var cleanConfigData : ConfigDataVO = parser.parseConfigData( rawConfigData );
configDataLoadedSignal.dispatch( cleanConfigData );
}
//RetrieveConfigDataCommand extends AsyncCommand
[Inject]
public var service : IAppConfigRetrieverService
public function execute() : void{
service.configDataLoadedSignal.addOnce( onConfigDataLoadedSignal );
service.loadConfigData();
}
private function onConfigDataLoadedSignal( configData : ConfigDataVO ) : void{
//do whatever you need to do with the config data
this.dispatchComplete( true );
}
/*-----------------------------------------------------
*
* Now, if you need to switch from XML to JSON all you have to do is:
*/
//JSONConfigDataParser implements IConfigDataParser
public function parseConfigData( rawData : String ) : ConfigDataVO{
var output : ConfigDataVO = new ConfigDataVO();
var decoder : JSONDecoder = new JSONDecoder();
var objData : Object = decoder.decode( rawData );
//-- loop through object properties and assign them to the VO properties, for example:
output.logoURL = objData.logoURL
//--
return output;
}
//wherever you do your mappings
//previously:
//injector.mapSingletonOf( IConfigDataParser, XMLConfigDataParser )
injector.mapSingletonOf( IConfigDataParser, JSONConfigDataParser )
//et voila, everything works as before
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment