2 Replies Latest reply on Aug 1, 2009 11:21 PM by roshni79

    Flex query on URLLoader.load()

    roshni79

      Hello all,

       

      I just started to learn flex/actionscript. I developed a simple flex project for experimenting with URLLoader.load() and used trace() to get the order in which methods are being called. I could not understand the order in which trace outputs are displayed. My code is :

       

      package LoadPack
      {
          import flash.events.Event;
          import flash.events.IOErrorEvent;
          import flash.net.URLLoader;
          import flash.net.URLRequest;
         
          import mx.controls.Alert;
         
          public class ApplInfo
          {
              private static var instance:ApplInfo = null;
              private var applInfoXML:XML = new XML();
              private var serverForAppl:String = "DataAccessLocalApplServer";
              private var applContextRoot:String = "DataAccessLocalApplContextRoot";
             
              private var selectedServer:String="";
              private var selectedServerContextRoot:String="";
              private var selectedServerLabel:String="";
              private var selectedServerContextRootLabel:String="";
             
              public static function getInstance():ApplInfo {
                  if(instance == null){
                      trace("ApplInfo instance is null");
                      instance = new ApplInfo();
                      trace("ApplInfo instance created");               
                  }
                  return instance;
              }
             
              public function ApplInfo()
              {
                  trace("inside ApplInfo constructor");
                  getApplProperties();
                  trace("selectedServer..getApplProperties -> "+this.selectedServer);
                  trace("selectedServerContextRoot..getApplProperties ->"+this.selectedServerContextRoot);
              }
             
              private function getApplProperties():void{           
                  trace("inside getApplProperties");           
                  var applXML:String = "assets/ApplInfo.xml";
                  var urlReq:URLRequest= new URLRequest(applXML);
                  var urlLdr:URLLoader= new URLLoader();
                  urlLdr.addEventListener(Event.COMPLETE, doOnComplete);
                  urlLdr.addEventListener(IOErrorEvent.IO_ERROR,doOnError);
                  trace("before load");
                  urlLdr.load(urlReq);
                  trace("after load");
              }
             
              private function doOnError(evt:Event):void{
                  Alert.show(evt.toString());
              }
             
              private function doOnComplete(evt:Event):void {
                  trace("inside doOnComplete");           
                  var urlLdr : URLLoader = evt.currentTarget as URLLoader;               
                  applInfoXML = new XML(urlLdr.data);           
                  for each(var serverXML:XML in applInfoXML.application.servers.server){
                      if(serverXML.@type == serverForAppl){
                          this.selectedServer = serverXML.text();
                          this.selectedServerLabel = serverXML.@label;
                      }
                  }
                  for each(var contextRootXML:XML in applInfoXML.application.contextroots.contextroot){
                      if(contextRootXML.@type == applContextRoot){
                          this.selectedServerContextRoot = contextRootXML.text();
                          this.selectedServerContextRootLabel = contextRootXML.@label;
                      }
                  }
                  trace("selectedServer..doOnComplete -> "+this.selectedServer);
                  trace("selectedServerContextRoot..doOnComplete -> "+this.selectedServerContextRoot);
              }
             
              public function getApplURL():String{
                  trace("selectedServer..getApplURL -> "+this.selectedServer);
                  trace("selectedServerContextRoot..getApplURL -> "+this.selectedServerContextRoot);           
                  return this.selectedServer + this.selectedServerContextRoot;           
              }
             
          }
      }

       

      I am calling this AS class from another class as

       

      ApplInfo.getInstance().getApplURL();


      and I am getting the following trace output.

       

      ApplInfo instance is null
      inside ApplInfo constructor
      inside getApplProperties
      before load
      after load

      selectedServer..getApplProperties ->
      selectedServerContextRoot..getApplProperties ->
      ApplInfo instance created
      selectedServer..getApplURL ->
      selectedServerContextRoot..getApplURL ->
      inside doOnComplete
      selectedServer..doOnComplete -> http://localhost:8090
      selectedServerContextRoot..doOnComplete -> /DataAccessProj


      My understanding is that once load completes its operation, the event handler for Event.complete will be executed and in my case the method doOnComplete(evt:Event) should be executed. After that only, the code proceed to subsequent statements. But trace output does not look that way.  I will be missing something seriously but could not find out.   I already spent one day on this. I am also attaching the project. Please help..