views:

166

answers:

1

Hi, I want to display an Flex component in several different places through out the application. And it should be the same instance of the component, but not the copies. So I think of making the Component as an Singleton.

But the problem is :

when I do something like this : var vb1: VBox = new VBox(); var vb2: VBox = new VBox();

var comp : MyComponent  = new MyComponent.getInstance();  

vb1.addChild(comp); 
vb2.addChild(comp);

The component is displayed only in "vb2" . I think when we call vb2.addChild(comp), it removes the child in vb1.

Do anyone have an idea about how to solve this problem ?

+2  A: 

If you want the component to be displayed at multiple places, then it will have to be copies, since it is the component itself that is displayed. Why do you need the component to be a Singleton? Is it only the underlying data that needs to come from the same place? In that case, you can just refer the display components to the same piece of data.

If you really do need the same component in multiple places, you will have to move it around whenever it is displayed.

Eg:

vb1.addChild(new MyComponent(MyData.getInstance()));
vb2.addChild(new MyComponent(MyData.getInstance()));
CookieOfFortune
This actually happened to me when I had to use a flash component on different screens. The flash component wasn't made by me, and it was built in such a way that having multiple instances of it resulted in strange behavior (I blame it on the singletons). Other than this, I can see no other scenario that would benefit from this solution. If it's a "in house" component, just make its instances bound to the same model.
bug-a-lot
Sorry, I don't understand well. What is MyData in your example? For me, I need to make MyComponent the same instance in "vb1" and "vb2"
maoanz