简体   繁体   中英

Reading server URL from ActionScript 3 for Cairngorm configuration

I need to read the URL that the browser shows when a Flex application is called because I would to reference it in a mxml configuring Cairngorm remote objects.

The goal I would reach is to automatically configure Cairngorm services from environment to environment (dev,test,qa,prod) without statically set the value in the mxml or other ActionScript. Since the Flex client is deployed in the root of the war of the webapp, it's enough to read where the browser is pointing.

I have written a class that is doing so:

public class ConfigServer {

        public function ConfigServer() {
            var loaderUrl:String = FlexGlobals.topLevelApplication.loaderInfo.loaderURL;
            var urlToSet:String = <loaderURL-string-manipulation>;
            _serverUrl = urlToSet;         
        }  

        private var _serverUrl:String = '';

        public function get serverUrl():String
        {
            return _serverUrl;
        } 

}

In my mxml I would do so:

     <mx:Script>
        <![CDATA[
            import org.fao.fapda.util.ConfigServer;

            private var configuration:ConfigServer = new ConfigServer();
        ]]>
    </mx:Script>

<mx:RemoteObject 
        id="userService"        
        destination="userService"  
        endpoint= "{configuration.serverUrl}/messagebroker/amf" 
        showBusyCursor="true" 
        requestTimeout="100"
        />

But whenever I call the ConfigServer constructor and for every (known to me) technique I applied (statics or singletons or public ro so on), I have always had the same error:

TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at org.fao.fapda.util::ConfigServer()[C:\dev\workspaces\FAPDA\trunk\FAPDA-client\src\org\fao\fapda\util\ConfigServer.as:8]

Cairngorm services initialization is done as follow:

<fx:Declarations>
            <cut/>
        <services:FAPDAServices id="services"/>
            <cut/>  
    </fx:Declarations>

and the problem is that FAPDAServices.mxml is read runs before FlexGlobals is valid...

Is there a point in the Flex Application lifecycle where such loaderURL is defined so that I can construct ConfigServer ? When in startup events that initialization in done?

I confess I'm a Flex epic rookie, so it's possible I'm completely wrong on this.

Best regards

I can't directly answer your question, but hopefully I can point you in the right direction until someone more experienced sees your question.

Create an event handler for the application tag's creationComplete event (or the highest mxml component tag if this is your own custom component) and instantiate ConfigServer there. It is generally neater to do initialization there since it is the last stop before anything is displayed on screen. You can read up more about the event in the adobe live docs. My paraphrasing should never be considered a replacement for official documentation.

You can also use the trace() statement to output text to the console to help you debug the order of execution and whether or not an object has been instantiated. Once again you can check the adobe live docs for more info.

Good luck.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM