DataLoader

Kind of class:class
Inherits from:Dispatcher
Classpath:org.asapframework.data.loader.DataLoader
File last modified:Monday, 09 October 2006, 23:33:37
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


Constructor
Class properties
Class methods
Instance methods

Constructor

DataLoader

private function DataLoader (
)

Constructor

Class properties

mInstance

static private mInstance:DataLoader
(read)

Instance properties

mPostXMLLoader

private mPostXMLLoader:LoadVarsXML
(read)

mXMLLoader

private mXMLLoader:XMLLoader
(read)

Class methods

getInstance

static function getInstance (

Returns:
  • singleton instance of Loader

Instance methods

addLoaderListener

function addLoaderListener (
inHandler:Function) : Void

Add specified function as handler for DataLoaderEvent events

handleDataLoaded

private function handleDataLoaded (
inData:Object, inName:String) : Void

Handle event that data has been loaded

handlePostDone

private function handlePostDone (
) : Void

Handle done event from the postXMLLoader

handlePostError

private function handlePostError (
) : Void

Handle error while loading from the postXMLLoader

handleXMLError

private function handleXMLError (
e:XMLEvent) : Void

Handle error while loading with the XMLLoader

handleXMLLoaded

private function handleXMLLoaded (
e:XMLEvent) : Void

Handle loaded event from xml loader

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