07.10
I came across a snag that took me by surprise while developing our Band Beast Administration. The admin has several views and forms in it and they all basically have the same logic, but different visuals. I write base classes all the time in my Flash development, but hadn’t attempted to do this with MXML components in Flex. I wrote my base component that would handle common logic and display common visuals and then I created my first component that would extend this base class. The MXML looks like this:
ChildView.mxml – extends BaseView
<BaseView xmlns=“*” xmlns:mx=“http://www.adobe.com/2006/mxml”
width=“400″ height=“300″>
</BaseView>
This works fine and when adding this component to my Application, it looked and worked the same as my base class. The next logical step, or so I thought, would be to start adding my visual elements in the MXML of my sub component.
<BaseView xmlns=“*” xmlns:mx=“http://www.adobe.com/2006/mxml”
width=“400″ height=“300″>
<mx:Text text=“I am a CHILD” />
<mx:ComboBox />
</BaseView>
When compiling, I got an unexpected error:
Error: Multiple sets of visual children have been specified for this component (base component definition and derived component definition).
So this means I can extend a MXML component, but I can’t add visual components to it? Seems strange to me; why else would you extend a component? After researching, I found out that allowing this approach was a hot debate when developing Flex Builder and Adobe ultimately decided not to allow this procedure. Instead, they suggest you use what they call ‘Component Templating’. To do this you must first wrap the visual children inside of your sub component inside a tag in your MXML. You can call this tag whatever you want but in this example I called it ’subComponents’.
<BaseView xmlns=“*” xmlns:mx=“http://www.adobe.com/2006/mxml”
width=“400″ height=“300″>
<subComponents>
<mx:Text text=“I am a CHILD” />
<mx:ComboBox />
</subComponents>
</BaseView>
Notice you’ll get the following error inside of Flex Builder
Error: Could not resolve subComponents to a component implementation
This is because you need to declare ’subComponents’ in your base component. At this point, ’subComponents’ is an array that holds all of the display children in your sub component. We need to declare this variable in our base class as well as create our Setter function so its value will be set.
our BaseClass – extends Canvas
<mx:Canvas xmlns:mx=“http://www.adobe.com/2006/mxml” width=“400″ height=“300″>
<mx:Script>
<![CDATA[
private var _subComponents:Array;
public function set subComponents(a:Array):void{
_subComponents = a;
}
]]>
</mx:Script>
<mx:Text text=“I am the MASTER” />
</mx:Canvas>
Now we’ve killed our error and Flex is happy. Lastly, we need to actually add these children to the stage by writing a function that fires on the ‘creationComplete’ event in our base class.
<mx:Canvas xmlns:mx=“http://www.adobe.com/2006/mxml” width=“400″ height=“300″ creationComplete=“addSubComponents()”>
<mx:Script>
<![CDATA[
private var _subComponents:Array;
public function set subComponents(a:Array):void{
_subComponents = a;
}
private function addSubComponents():void{
for(var i:int=0; i < _subComponents.length; i++) {
addChild( _subComponents[i] );
}
}
]]>
</mx:Script>
<mx:Text text=“I am the MASTER” />
</mx:Canvas>
Now we’ve successfully extended a MXML component. This may sound like a ‘work around’, but in the end it makes sense. Plus the cool part is that now we can add these sub components inside a styled or skinned container — hence the title ‘Component Templating’.
someCoolContainer.addChild( _subComponents[i] );
nice workaround
Karfau could you show code for this please?
I have always overridden the addChildAt() method in the base MXML class and then added my base elements there if the child length was 0. This method allows you to keep them in MXML though, which is nice.
Can you have child elements inside those elements though?
Would this work:
I’m not sure how complex your view logic is, but when subclassing MXML components, it’s often easier to do the following:
BaseClass.mxml
Another note, when you’re creating AS classes that will be implemented in MXML, use the [DefaultProperty("text")] tag so that you don’t need to explicitly use the tag in the impl.
Hope that helps
Pillspot.org. Canadian Health&Care.No prescription online pharmacy.Special Internet Prices(up to 40% off average US price).PillSpot.org. Vitamins@buy.online” rel=”nofollow”>.…
Categories: Pain Relief.Blood Pressure/Heart.Mens Health.Stomach.Skin Care.Eye Care.Vitamins/Herbal Supplements.Antidiabetic.Antidepressants.Anxiety/Sleep Aid.Mental Health/Epilepsy.Antibiotics.General Health.Cholesterol.Anti-allergic/Asthma.Stop …
Buy:Propecia.Maxaman.Viagra.Viagra Professional.Viagra Super Active+.Viagra Super Force.Cialis.Tramadol.VPXL.Soma.Cialis Professional.Levitra.Cialis Soft Tabs.Viagra Soft Tabs.Cialis Super Active+.Zithromax.Super Active ED Pack….
Buy:Cialis.Tramadol.Viagra Soft Tabs.Propecia.Viagra Super Force.Viagra Professional.Zithromax.VPXL.Soma.Viagra.Cialis Professional.Levitra.Maxaman.Cialis Soft Tabs.Cialis Super Active+.Viagra Super Active+.Super Active ED Pack….
110 Sony cdx/ http://BESTPARTSPLUS.INFO/tag/GT : GT…
GT…
Light http://qcheapssixvq.ALLSTOCKSPORT.INFO/tag/without meter Light Photography/ : Photography…
Photography…
12 http://vnomafa8bap8.ALLSTOCKSPORT.INFO/tag/case R2 12/ : case…
R2…
league http://iiplsiwnvr8.ALLSTOCKSPORT.INFO/tag/indian+premier+league+IPL+ipl/ : league…
premier…
sigma http://csigmau-evbh.AACEHARDWARE.INFO/tag/af+50+500+telephoto+lens+sigma+zoom/ : 500…
lens…
place http://tone1dua.AWESOMEBABYCLOTHES.INFO/tag/westmont+place+townhomes+cruz+one/ : one…
one…
coated http://ucarxubmst.ANTIQUEFURNINISHING.INFO/tag/card+stock+Paper+coated/ : stock…
coated…
De http://pantonfrsnmc.AUTOTECHGUIDE.INFO/tag/Giada+De+Laurentiis+nigella+Lawson/ : Lawson…
Lawson…
Printer http://ldigitalh5nfj.ALLSTOCKSPORT.INFO/tag/Canon+Selphy+Cp740+Compact+Photo+Printer+canon+printer/ : Cp740…
Cp740…
saw http://qfortiesdm4tqj.03GMCPARTS.US/tag/delete+R2+saw/ : saw…
saw…