incubator-flex-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Kessler CTR Mark J" <mark.kessler....@usmc.mil>
Subject RE: Using FlexGlobals.topLevelApplication in class init code (or: "how do you set default styles for an AS3 component?")
Date Mon, 03 Dec 2012 16:10:17 GMT
Just a quick drive by...

If I get nulls in a component that I'm trying to reference early, I can usually change to
a later event such as the creationComplete event or try accessing the parent a little different.
 You could try a parentApplication reference maybe?

-----Original Message-----
From: Russell Warren [mailto:russ@perspexis.com] 
Sent: Monday, December 03, 2012 10:21
To: flex-users@incubator.apache.org
Subject: Using FlexGlobals.topLevelApplication in class init code (or: "how do you set default
styles for an AS3 component?")

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?

Thanks,
Russ

<<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 =
FlexGlobals.topLevelApplication.styleManager.getStyleDeclaration(fqpn);
        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!>

}

Mime
View raw message