Why my composite component adds an internal component twice?

Once, I tried to add a component into my composite component by overriding its createChildren method.

I have added it successfully but wait, sometimes, it added this component twice.

This was remarkably surprising. Then I investigated this issue and figured that out:

Internal component might be added by the super class before createChildren method of your subclass. So we have two options to prevent this weird behavior:

1- Be sure that you call super.createChildren() in the very end of subclasses overridden createChildren method:

override protected function createChildren():void{

var objectList:DropDownList = new DropDownList();
addChild(objectList);
super.createChildren();
}

2 – Null check the component you wanna add after defining it as a class property:

protected var objectList:DropDownList = new DropDownList();

override protected function createChildren():void{

if(!objectList){
addChild(objectList);
}

super.createChildren();
}

 

Yorum yapın

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Değiştir )

Twitter picture

You are commenting using your Twitter account. Log Out / Değiştir )

Facebook photo

You are commenting using your Facebook account. Log Out / Değiştir )

Connecting to %s

Follow

Get every new post delivered to your Inbox.