Making sure your Flex is initialized 4

Posted by Neil on March 20, 2008

I've just banging my head against a brick wall for the past half an hour trying to work out why a bit of Flex MXML was crashing as soon as it was loaded. I had a situation whereby a label component was coming back as null once called from an event handler. Dipping into the debugging view confirmed this.

The issue? Well, the event was fired as soon as it's parent component was loaded, which was also before the Label had started to load (note that the order of nodes in your MXML does make a difference). Consider the following code:

[The requested file http://neilmiddleton.com/Mods/http://neilmiddleton.com/Mods/2008/03/in itialized.txt could not be found]

By using this.initialized you are able to check that the parent node has loaded (in this case, the Application, but sometimes the containing component), and therefore run your code relatively safely.

I guess best practise would say that you should always check for initialization signatures regardless - better safe than sorry.

Trackbacks

Use this link to trackback from your own site.

Comments

Leave a response

  1. Eric Cancil Thu, 20 Mar 2008 19:45:00 EDT

    Or you could choose to make things a lot more rigid and have it more like this

    private var _theText:String;
    private var theTextDirty:Boolean = false;
    public var label:Label;

    public function set theText(val:String):void{
    _theText = val;
    theTextDirty = true;
    invalidateProperties();
    }

    override protected function commitProperties():void{

    super.commitProperties();

    if(theTextDirty){
    theTextDirty = false;
    label = _theText;
    }
    }

  2. Eric Cancil Thu, 20 Mar 2008 19:45:41 EDT

    oops forgot the getter

    public function get theText():String{
    return _theText;
    }

  3. Joeflash Thu, 20 Mar 2008 22:22:33 EDT

    Wouldn’t it be a better idea to disable user interaction until the Application.creationComplete event has been called?

  4. Neil Tue, 25 Mar 2008 15:03:17 EDT

    @Joe indeed - but it’s always better to be safe than sorry ;)

Comments