R – Flex: Loading assets into externally loaded modules

actionscript-3apache-flexflash

So, I have Flex project that loads a Module using the ModuleManager – not the module loader. The problem that I'm having is that to load an external asset (like a video or image) the path to load that asset has to be relative to the Module swf…not relative to the swf that loaded the module.

The question is – How can I load an asset into a loaded module using a path relative to the parent swf, not the module swf?


Arg! So in digging through the SWFLoader Class I found this chunk of code in private function loadContent:

    // make relative paths relative to the SWF loading it, not the top-level SWF
    if (!(url.indexOf(":") > -1 || url.indexOf("/") == 0 || url.indexOf("\\") == 0))
    {
         var rootURL:String;
         if (SystemManagerGlobals.bootstrapLoaderInfoURL != null && SystemManagerGlobals.bootstrapLoaderInfoURL != "")
              rootURL = SystemManagerGlobals.bootstrapLoaderInfoURL;
         else if (root)
              rootURL = LoaderUtil.normalizeURL(root.loaderInfo);
         else if (systemManager)
              rootURL = LoaderUtil.normalizeURL(DisplayObject(systemManager).loaderInfo);

              if (rootURL)
              {
                   var lastIndex:int = Math.max(rootURL.lastIndexOf("\\"), rootURL.lastIndexOf("/"));
                    if (lastIndex != -1)
                         url = rootURL.substr(0, lastIndex + 1) + url;
               }
          }
}

So apparently, Adobe has gone through the extra effort to make images load in the actual swf and not the top level swf (with no flag to choose otherwise…), so I guess I should submit a feature request to have some sort of "load relative to swf" flag, edit the SWFLoader directly, or maybe I should have everything relative to the individual swf and not the top level…any suggestions?

Best Answer

You can import mx.core.Application and then use Application.application.url to get the path of the host application in your module and use that as the basis for building the URLs.

For help in dealing with URLs, see the URLUtil class in the standard Flex libraries and the URI class in the as3corelib project.

Related Topic