tags:

views:

111

answers:

2

I have a class called JDChart, and a class called JDLine. Inside JDChart there is a method called addLine() that expects 1 parameter of type JDLine. This is all good. but I want to be able to put this in XML Like this:

<JDChart>
<JDLine/>
<JDLine/>
<JDLine/>
</JDChart>

And for each JDLine nested in a JDChart in the MXML, I want the addLine() method to be called on the JDChart with the respective JDLine passed.

Does what I want to do make since? I am not sure how to set this up? I am assuming I have to use meta tags on the JDChart class somewhere to tell the compiler to do this? Does anyone know?

Thanks!!

A: 

I believe when you add things in MXML like that it will just construct them and then call addChild().

You could have JDChart override addChild(), and check the type of what's being added. If it's a JDLine you can then pass it to your addLine() method before passing it along to super.addChild().

Herms
Thanks, I tried this but I get the "Multiple sets of visual children have been specified for this component" Error. Even though I am not even adding them because I never call super.addChild() if the type is of JDLine.
John Isaacks
Is that a run or compile time error?
Herms
It is a runtime error, I looked it up and the reason is because the JDChart is a custom component which already has children, and you can't add children to a custom component that already has children. However the JDLine is really more of a dataProvider, (well it tells it which info from the data provider to graph) it doesnt actually get added to the display list. But you need to be able to add multiples, each representing a line to be to be graphed on the chart.
John Isaacks
A: 

If JDLine objects are going to be parented only by JDChart objects, use this.

In the added event handler of the JDLine class, add the following code:

public function onAdded(e:Event):void
{
  var chart:JDChart = this.parent as JDChart;
  if(!chart)
    throw new Error("Parent is not JDChart");
  chart.addLine(this);
}
Amarghosh