As you've discovered, properties specified
as MXML attributes get set BEFORE the children of a component are created. This
is intentional; in the case that you are creating children by overriding
createChilden(), it allows you create only the appropriate children for a
complex component. For example, imagine a Clock component with a 'mode'
property than can be "analog" or "digital"; the two modes
would require different children. Therefore, the recommend pattern for
implementing a property is for the setter to set a storage variable and a
change flag and then call invalidateProperties(). Latter, in commitProperties(),
which gets called AFTER the children have been created, commit the new property
value into the child and clear the change flag. If you look at the framework
source code, you'll see this pattern used for many properties of UIComponents. private var _ myProperty:String
= "default value"; private var
myPropertyChanged:Boolean = false; public function
get myProperty ():String {
return _myProperty; } public function
set myProperty(value:String):void {
_myProperty = value;
myPropertyChanged = true;
invalidateProperties(); } override
protected function commitProperties():void {
super.commitProperties();
if (myPropertyChanged)
{
myText.text = _myProperty;
myPropertyChanged = false;
} } Another advantage of this approach is that
you can set myProperty many times in a method (for example, in a loop which
builds it up by concatenation), but myText.text will get set only once, when
the LayoutManager calls commitProperties(). This optimizes performance because
setting the text of a TextField is a slow operation; for example, it requires a
multiline TextField to figure out all its line breaks. - Gordon From:
flexcoders@yahoogroups.com [mailto:[EMAIL PROTECTED] On Behalf Of Lachlan Cotter Hi List, I have a question about the initialisation rules for MXML components.
My situation is as follows: I have defined a custom MXML component that extends the Canvas class
(myComponent) MXML root node of the file, I put a Text element, assigning it an ID
(myText). I define a setter method on this class (myProperty) that takes a String
argument and passes it to myText.text. I use the custom component in the application, with a tag like this: <local:MyComponent myProperty=" Sure enough the instance is created and the setter method is called.
However, an error results when I try to access the property of the child Text
object. Apparently it has not been initialised when the method is called. This
results in an error (setting a property on a null object). My question is if this is the expected behaviour and if there is a way
to achieve this with MXML (setting properties of child objects from a setter
method called at init time). Thanks for any insights. Cheers, Lach
-- Flexcoders Mailing List FAQ: http://groups.yahoo.com/group/flexcoders/files/flexcodersFAQ.txt Search Archives: http://www.mail-archive.com/flexcoders%40yahoogroups.com
SPONSORED LINKS
YAHOO! GROUPS LINKS
__,_._,___ |