I have an XMLLoader class that I wrote that I've been using to load XML
files.

------------------------------------------------------------------------------------------------------------------------------------------------------
 public class XMLLoader{

/**
* Load an XML file
*
* @param url The path to the XML file.
* @param onComplete The function to call when the XML file loads, passes
back an argument of type StyleSheet
* @param onError The function to call (defaults to null) if the load errors
out, passes back an IOErrorEvent object.
*/
 public static function load(url:String, onComplete:Function,
onError:Function = null):void{
var _xml:XML = null;
var _xmlURL:URLRequest = new URLRequest(url);
var _xmlLoader:URLLoader = new URLLoader(_xmlURL);

_xmlLoader.addEventListener(Event.COMPLETE, xmlLoaded);
_xmlLoader.addEventListener(IOErrorEvent.IO_ERROR, xmlError);

function xmlLoaded(e:Event):void{
 _xmlLoader.removeEventListener(Event.COMPLETE, xmlLoaded);
_xmlLoader.removeEventListener(IOErrorEvent.IO_ERROR, xmlError);
 if(onComplete is Function){
var xml:XML = XML(_xmlLoader.data);
onComplete(xml);
}
}
 function xmlError(e:IOErrorEvent):void{
 _xmlLoader.removeEventListener(Event.COMPLETE, xmlLoaded);
_xmlLoader.removeEventListener(IOErrorEvent.IO_ERROR, xmlError);

if(onError is Function){
onError(e);
}
}
}
}
------------------------------------------------------------------------------------------------------------------------------------------------------

As you can see it doesn't do too much, but writing the following every time
I wanted to load an XML file was getting tedious:

import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.net.URLRequest;
import flash.net.URLLoader;

var xmlLoader:URLLoader = new URLLoader(new
URLRequest("path/to/xmlFile.xml"));

xmlLoader.addEventListener(Event.COMPLETE, onXMLLoaded);
xmlLoader.addEventListener(IOErrorEvent.IO_ERROR, onXMLError);

function onXMLLoaded(e:Event):void{

}

Being able to call

XMLLoader.load("path/to/xmlFile.xml", onXMLLoaded, onXMLError);

is so much better, the question is I am no longer using events, and handling
the event listeners within the XMLLoader class and passing functions to the
class to call upon said events.I'm curious as to other people's thoughts on
this in terms of good/bad practice and what the pros/cons to this approach
might be.

Just want to hear all your thoughts.

Thanks,
Taka
_______________________________________________
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Reply via email to