views:

447

answers:

2

In my application, I have a chart that I want to display in a TitleWindow when clicked on.

        var win:TitleWindow = PopUpManager.createPopUp(this, TitleWindow, false) as TitleWindow;
        win.addChild(myChart);
        PopUpManager.bringToFront(win);

It does indeed place the chart in the titlewindow that shows up, but it removes the original chart from the parent. Then, when the titlewindow is closed, my chart is simply gone. I can't figure out how to clone the chart -- all the methods I've tried failed -- and I have no idea why this is happening.

Solution:

  public var barChart:BarChart;
  public function onClick(e:Object):void
  {
    barChart = (e as BarChart);
    if(barChart != null)
    {
        var win:MyWindow = PopUpManager.createPopUp(this, MyWindow, false) as MyWindow;
        PopUpManager.centerPopUp(win);

    }
  }

// ... MyWindow.mxml ...

var _parent:Object;
private function creationComplete(e:Event):void
{
 bChart = parentApplication.barChart;
 _parent = bChart.parent;
 this.addChild(bChart);
}

private function onMyWindowClose(evt:CloseEvent):void {
    _parent.addChild(bChart);
    PopUpManager.removePopUp(this);
}
A: 

I haven't managed to find documentation to support it, but I believe that a Flash UI component can only have a single parent. When you call addChild() I suspect that the parentage of the control changes and it disappears from the other window. Since the parentage has changed it will get garbage collected when the new TitleWindow disappears.

What I think I would do in your position would be to abstract the chart out to its own control so you don't have to duplicate code, use the same control in your regular window and your popup, and pass the data to it after the createPopUp call.

Simon
The chart is generated and embedded in a custom component. The data is processed in that component and the only way I can use the data is to pull the chart from the component after it is generated.
FlexMan
That's unfortunate, it sounds like you have a more profound architectural issue which is somewhat at odds with how Flex/Flash works. @Patrick's answer may work, but I think you might do well to make a cleaner separation between data and presentation.
Simon
A: 

When you do an addChild the object is reparented, as said Simon one DisplayObject can't belong to two parent. If is s not a problem to not seeing two chart in the same time when you open your popup, you can before you open the popup save the child parent and index, and when you close the popup restore the child state.

before opening

myChartParent=myChart.parent;
myChartIndex=myChartParent.getChildIndex(myChart);

when closing

myChartParent.addChildAt(myChartIndex);

But if you want to see two chart you will have to create two chart instance.

Patrick
Heh, I came here to post this exact solution.
FlexMan