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 xmlns="http://ns.adobe.com/mxml/2009">
<Script>
<![CDATA[
import com.caspar.model.Globals;
]]>
</Script>
<Rect height="{Globals.SIDE_PANEL_HEIGHT}"/>
</Group>
Related posts:
- Flex TitleBox Component For a recent project I was working on, I needed...
- How to Define a RemoteObject in Actionscript For the project I have been recently working on, I...
- How to Print a Component to Disk in Flex Here is an example of how to print a component...
- Flex AJAX Bridge I’m at MAX2007, sitting in Andre Dragomir’s session, Integrating Flex...
- How To Make A DataGrid Row Change On Hover Here is a wee example that shows how to get...
Related posts brought to you by Yet Another Related Posts Plugin.











|
Posted by charmer on July 1, 2009 at 11:12 pm

Is there no other way to do it? Like a #define in C++ ?
Thanks man, nice and clean.