DataLoader
| Kind of class: | class |
|---|---|
| Inherits from: | Dispatcher |
| Classpath: | org.asapframework.data.loader.DataLoader |
| File last modified: | Wednesday, 11 October 2006, 21:56:59 |
Class for loading XML-data with or without posting parameters in the request to the server.
This class can be used to load static XML files or dynamic XML files based on parameters, or to post forms that give XML back as a result.
Identification of a request is done by a unique name, that is passed in the request, and returned in the response.
XML data is parsed into an Object instance by means of XML2Object.parseXML. This object is returned in the event that is sent when the data has been received and parsed.
An event of type DataLoaderEvent.DATA_LOADED is dispatched when data has been loaded successfully. This event contains the name of the original request, and the parsed data.
In case of an error, an event of type DataLoaderEvent.ERROR is dispatched, with the name of the original request, but without any data.
The function addLoaderListener can be used to add a handler for all DataLoaderEvent events. The handler can be removed with removeLoaderListener.
Usage:
This is an example of code in a DataManager class that uses the DataLoader:
// Private constructor for use as Singleton private function DataManager() { mDataLoader = DataLoader.getInstance(); mDataLoader.addLoaderListener(EventDelegate.create(this, handleDataLoaded)); } // load XML from specified url with specified name public function loadXMLData (inURL:String, inName:String) : Boolean { var ud:URLData = new URLData(inName, inURL); return mDataLoader.loadXML(ud); } // load settings xml public function loadSettings () : Void { loadXMLData(URL_SETTINGS, URLNames.SETTINGS); } // Handle data loaded event from data loader private function handleDataLoaded (e:DataLoaderEvent) : Void { Log.debug("handleDataLoaded: name = " + e.name, toString()); // check error if (e.type == DataLoaderEvent.ERROR) { handleDataError(e); return; } switch (e.name) { case URLNames.SETTINGS: handleSettingsLoaded(e.data, e.name); break; } } // Handle error event from data loader private function handleDataError (e:DataLoaderEvent) : Void { var evt:DataEvent = new DataEvent(DataEvent.LOAD_ERROR, e.name, null, this); evt.error = "Failed to load the XML file named " + e.name; dispatchEvent(evt); } // Handle settings loaded private function handleSettingsLoaded (inData:Object, inName:String) { // parse object }
Summary
Class methods
Instance methods
addLoaderListener
function addLoaderListener (
inHandler:Function) : Void
Add specified function as handler for DataLoaderEvent events
loadXML
function loadXML (
inURLData:URLData,
inLV:LoadVars) : Boolean
Load data, with or without post
Parameters:
inURLData:
URLData object containing name and url to load from
inLV :
LoadVars object with post info; if left out, a regular request is performed
Returns:
true if loading was started successfully, otherwise false
removeLoaderListener
function removeLoaderListener (
inHandler:Function) : Void
Remove specified function as handler for DataLoaderEvent events
toString
function toString (
) : String