There are several ways to define a set of global constants in your Flex app. You can use a bunch of “include” statements or you can create a singleton object to hold your data. However, probably the best way to define a set of global constants is to create an object with all static constants as members. You don’t have to refer to any instances of the object, you don’t have to instantiate it, and the object will be available wherever you import it. Here is an example of a Global object:
Globals.as
package com.caspar.model{
public class Globals {
public static const EXAMPLE_1_TEXT:String = "Some Text";
public static const EXAMPLE_2_TEXT:String = "Some More Text";
public static const APP_WIDTH:Number = 1200;
public static const APP_HEIGHT:Number = 800;
public static const BORDER_SIZE:int = 13;
public static const SIDE_PANEL_WIDTH:int = 330;
public static const SIDE_PANEL_HEIGHT:int = APP_HEIGHT - ( 2* BORDER_SIZE );
public static const MAIN_PANEL_WIDTH:int = APP_WIDTH - (SIDE_PANEL_WIDTH + ( 3 * BORDER_SIZE ) );
public static const MAIN_PANEL_HEIGHT:int = SIDE_PANEL_HEIGHT;
}
}
As you can see here, you can strongly type your constants, and you can perform basic math and concatenations in your constant declarations. This can be very useful when you have a lot of constants defined – if you have defined your globals carefully, you should only have to change a few values. To use the object, import it as shown below: Global Object Usage:
<Group >
<Script>
<![CDATA[
import com.caspar.model.Globals;
]]>
</Script>
<Rect height="{Globals.SIDE_PANEL_HEIGHT}"/>
</Group>
Have questions? Let us know in a comment below, or contact our team directly. You can also check out our other posts on customizing your Salesforce solution.
Is there no other way to do it? Like a #define in C++ ?
Thanks man, nice and clean.
This is a good blog, I was wondering if I could use this summary on my website, I will link it back to your website though. If this is a problem please let me know and I will take it down right away.
Wonderful composition. Remember to keep up the very superb work.