flex-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Russell Warren <r...@perspexis.com>
Subject Using FlexGlobals.topLevelApplication in class init code (or: "how do you set default styles for an AS3 component?")
Date Mon, 03 Dec 2012 15:21:11 GMT
When extending a component, the way I have been setting the default styles
is to access FlexGlobals.topLevelApplication.styleManager in some class
initialization code.  Something like this reduced code snippet:

http://static.inky.ws/syn/630  (also pasted below)

This method used to work, but some code that does this has now started
failing because when the class initialization code executes,
FlexGlobals.topLevelApplication has not been assigned yet and is still
null.  I can't yet figure out what changed in the code to cause this.

Digging around on this the order of execution for class init and
topLevelApplication assignment isn't clear to me, and I've seen the
mystical [MIXIN] tag involved, but I tried it with no luck.

My question is split, then...

1. Why would FlexGlobals.toplEvelApplication not be set yet, and how can I
fix it so that it works again?
2. Is there a better way to set the default styles on an AS3 Flex component?


<<pastebin'd code below, probably with horrible wrapping>>

public class AutocompleteItemRenderer extends LabelItemRenderer {
    private static const PADDING:Number = 7;

    private static var classInitComplete:Boolean = doClassInit();
    private static function doClassInit():Boolean {
        var defaultCSSStyles:Object = {
            paddingLeft: PADDING,
            paddingTop: PADDING,
            paddingRight: PADDING,
            paddingBottom: PADDING,
            verticalAlign: "middle"

        // quick debug check on FlexGlobals.topLevelApplication shows it is
now null?
        var tla:Object;
        tla = FlexGlobals.topLevelApplication;   //this is null now!?

        var fqpn:String; //fully qualified path name
        fqpn = "itemrenderers.AutocompleteItemRenderer";
        var cssStyleDeclaration:CSSStyleDeclaration =
        if (!cssStyleDeclaration) {
            cssStyleDeclaration = new CSSStyleDeclaration(fqpn,
FlexGlobals.topLevelApplication.styleManager, true);
        // set the defined styles as default...
        for (var i:String in defaultCSSStyles) {
            if (cssStyleDeclaration.getStyle(i) == undefined) {
                cssStyleDeclaration.setStyle(i, defaultCSSStyles [i]);
        return (true);

    // <snip!>


  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message