views:

141

answers:

3

In my flex app I have various custom components done with mxml or actionscript. I want all of them to extend a base-class where I can define properties/event listeners etc. Can someone give me an example how to create that base class and how I can extend it in mxml and actionscript components?

A: 

In the example below the component extends Form to create an address form .

Instead of form you can extend your own component .

When using actionscript i would suggest investigating flex components lifecycle for best performance : http://livedocs.adobe.com/flex/3/html/help.html?content=ascomponents_advanced_2.html

mx:Form xmlns:mx="http://www.adobe.com/2006/mxml" xmlns:MyComp="*"

<mx:FormItem label="NameField">
    <mx:TextInput/>
</mx:FormItem>

<mx:FormItem label="Street">
    <mx:TextInput/>
</mx:FormItem>

<mx:FormItem label="City" > 
    <mx:TextInput/>
</mx:FormItem>

<mx:FormItem label="State" > 
    <MyComp:StateComboBox/>
</mx:FormItem>

The following application file references the AddressForm component in the

AddressForm tag:

mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
xmlns:MyComp="*"

<MyComp:AddressForm/>

/mx:Application

from http://livedocs.adobe.com/flex/3/html/help.html?content=mxmlcomponents_1.html

Eran
But some of actionScript based components already built-in flex components. like this:public class MyButton extends Button{}How do I make those extend the new BaseClass?
Q-rius
If you are looking for multiple inharitance like in java , actionscript does not support it . you can use mutiple inerfaces which might help you MyButton extends Button { } implements ICustomButtons , IEventDispacher
Eran
A: 

Creating the base class:

ActionScript

In BaseClass.as:

public class BaseClass
{
}

Extending from the base class:

ActionScript

public class SubClass extends BaseClass
{
}

MXML

In a file called SubClass.mxml:

<ns:BaseClass xmlns:ns="path.to.base.*">
</ns:BaseClass>
Wesley Petrowski
But some of actionScript based components already built-in flex components. like this:public class MyButton extends Button{}How do I make those extend the new BaseClass?
Q-rius
That would require multiple inheritance, which ActionScript doesn't officially support. There is a way to get something that's pretty close using #include, but it's kind of messy. See http://www.darronschall.com/weblog/2006/10/multiple-inheritance-in-actionscript-3.cfm for more details.
Wesley Petrowski
+1  A: 

Maybe you could write a common interface for your components, just with the methods they need to implement

public interface ICustomComponent {

    function doSomething():void; 

    // more methods here

}

And then in your AS components you just implement the ICustomComponent interface (or however you named it)

public class CustomButton extends Button implements ICustomComponent {

    public function doSomething():void {

    }
}

You can do this in MXML components too :

<mx:Button xmlns:mx="http://www.adobe.com/2006/mxml"
    implements="ICustomComponent">

    <mx:Script>
        <![CDATA[

            public function doSomething():void {
                // blah blah
            }               

        ]]>      
    </mx:Script>

</mx:Button>

Just an idea. Hope it helps Cheers

just_a_dude