incubator-flex-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Perspexis <r...@perspexis.com>
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 23:05:33 GMT
Thanks for all the great info!  I'll stick with the CSS route since that is the favored way,
and that is how the core framework does it.


On Dec 3, 2012, at 1:00 PM, Alex Harui <aharui@adobe.com> wrote:

> 
> 
> 
> On 12/3/12 8:50 AM, "Russell Warren" <russ@perspexis.com> wrote:
> 
>> Thanks for the quick response...
>> 
>> On Mon, Dec 3, 2012 at 11:17 AM, Alex Harui <aharui@adobe.com> wrote:
>> 
>>> 1. FlexGlobals.topLevelApplication points to the Application instance, so
>>> it
>>> can't be assigned until the Application is instantiated, which requires
>>> that
>>> the class and instance initializers for Application get run.  If one of
>>> those things requires your custom class, then your class initializers will
>>> get run before topLevelApplication can get set.  So, using FlexGlobals in
>>> class initializers is not guaranteed to work.
>>> 
>> 
>> I wondered if it was something like this, thanks.  It seems it was luck
>> that it worked before, or something changed in the higher level application
>> that required earlier component instantiation.
>> 
>> I will now stay away from FlexGlobals in class initializers!
> Well, lots of times you don't have a reference to a custom class in a class
> or instance initializer of the Application.  For example:
> 
> <s:Application>
>    <MyCustomComponent>
> </s:Application>
> 
> In this code, the references to MyCustomComponent are all in code run after
> the Application is instantiated.
> 
> But I believe if you give it an id, or declare a variable of that type, then
> the custom class's initializers will run when when the Application's
> initializers run.
> 
>> 
>> 
>>> 2. In the most general case, styles should get applied per moduleFactory.
>>> Your style initializers are assuming that the styles should always apply to
>>> the topLevelApplication.  In really large, multi-swf apps, this kind of
>>> code
>>> can fail to work correctly.  But hey, if you are only writing single-swf
>>> apps, then something like this will work.
>>> 
>> 
>> This will definitely be used in a large multi-swf/Module application.  I'm
>> just after the right way to set defaults, regardless of application scope.
> Then you pretty much should use CSS unless this custom component will always
> be used in the main application.
>> 
>> 
>>> For the general case, the recommended practice is to declare a CSS
>>> type-selector for the component.  If you bundle the component in a SWC,
>>> then
>>> you include the CSS in the swc.
>>> 
>> 
>> Does this mean that *every* style property of *every* component in Flex has
>> a matching CSS assignment in a CSS file somewhere in the Flex library?
> Yup
>> What if you miss one?  What default is then used?
> Null or undefined.  And usually you find out right away because you get a
> null exception.  The key is to ship with a default CSS in the SWC.  Then
> there should be a default value if someone tries to override only some
> subset of the values.
> 
>> 
>> If you want to make the assumption that you can get away with shoving the
>>> styles into the top-level Application's StyleManager, I think you can just
>>> call StyleManager.getStyleManager(null).
>>> 
>> 
>> Interesting/awesome - I changed the code to
>> use StyleManager.getStyleManager(null) and what was broken is now
>> functioning again:
>> http://static.inky.ws/syn/631
>> 
>> Will this be "guaranteed to work" in a class initializer (whereas
>> FlexGlobals won't), or am I rolling the dice again here?
>> 
> Yeah, forgot about SystemManagerGlobals.  So you really should be using CSS,
> but another short term hack is to set this up in instance code (not static
> code).  Leave a static flag about whether you ran the style setup code and
> check it in the constructor of the class.  In theory the constructor will
> not run until after the Application and SystemManager globals have been
> setup (unless you instantiate one in the Application's instance initializers
> via
> <fx:Script>
>    var foo:MyCustomComponent = new MyCustomComponent();
> </fx:Script>
> 
> -- 
> Alex Harui
> Flex SDK Team
> Adobe Systems, Inc.
> http://blogs.adobe.com/aharui
> 

Mime
View raw message