tags:

views:

25

answers:

2

I am trying to set some public static constants on a class conditionally by passing variables to the compiler e.g. -define=CONFIG::ENVIRONMENT,'testing_server'

This is what I'd like to do:

if(CONFIG::ENVIRONMENT=='production')
    public static const DOMAIN:String="production_site.com";
else if(CONFIG::ENVIRONMENT=='testing_server')
    public static const DOMAIN:String="secret_domain.com";

I have tried many versions of this code, but everything so far has produced an error of one sort or another. The only way I have succeeded is by setting a compiler variable for each environment (all false except the one wanted which is true) and using the following syntax:

CONFIG::PRODUCTION{
    public static const DOMAIN:String="production_site.com";
}
CONFIG::TESTING_SERVER{
    public static const DOMAIN:String="secret_domain.com";
}

Obviously this means I have to tinker with long command line setting each time. I thought my initial approach was possible given the documentation and various tutorials I have read.

Can anyone help?

+1  A: 

One thing that has worked well for us in this situations is #include. Use the build script to generate a single line of AS which has the declaration. Then use #include to include the generated AS file in a class.

I wouldn't recommend widespread use of includes, they're ugly and can lead to problems, but very small usages can be useful in certain situations.

Sam
Perfect!I haven't checked, but I assume that the various environment files that define the constants will not be added to the compiled swf if they have not been included?
@user339681, right, non-included files are ignored.
Sam
A: 

It is. I'm doing something similar in my code:

trace('appMode = ' + CONFIG::appMode); 
if (CONFIG::appMode == "dev" || CONFIG::appMode == "tst") { 
    // if dev or tst
} else {
    // if anything else
}

I assign the constant appMode in fb4 using the "Additional Compiler Arguments" field in the Flex Compiler properties (project -> properties -> Flex Compiler). The argument looks like:

-define+=CONFIG::appMode,"'dev'"

Note the nested double/single quotes around dev.

Good Luck!

Rydell