views:

86

answers:

5

Hi,

This could potentially be a dumb question so apologies in advance if it is. I'm wondering if theres an equivilant of Interfaces in MXML?

Everytime I feel the need to use an interface I always wind up making an actionscript and not an MXML file because I don't know if / how you can.

For example I was going to have a component based on vbox. I have 4 different implementions of the same thing so I decided to use an interface. But instead of making a single MXML interface and implementing it I've created an interface in as3. I've implemented this interface in 4 different classes.

I then have made 4 different vbox containers each with one of the different implementations in the script tag.

Does this sound like a reasonable approach or am I going against the grain here?

EDIT -- adding examples

The interface

package components.content.contents
{
    public interface IContent
    {
        function init():void;
        function doSearch():void
        function setSearchTerm(term:String):void
    }
}

Implementation (1 of 4)

package components.content.contents
{
    public class ClipContent extends AbstractContent implements IContent
    {
        public function ClipContent()
        {
        }

        public function init():void
        {
        }

        public function doSearch():void
        {
        }

        public function setSearchTerm(term:String):void
        {
        }

    }
}

MXML File (1 of 4)

<?xml version="1.0" encoding="utf-8"?>
<mx:VBox xmlns:mx="http://www.adobe.com/2006/mxml" width="400" height="300">
        <mx:Script>
            <![CDATA[
                              // ClipContent Container
                import components.content.contents.ClipContent;
                public var content:ClipContent= new ClipContent()

                public function dostuff():void

                {
                  content.init()
                  content.doSearch()

                }
            ]]>
        </mx:Script>

</mx:VBox>
+1  A: 

No! MXML is a Declarative language for layout and positioning. By definition it needs an implementation. Interfaces are the definition of an API without an implementation.

It sounds like you're doing things exactly how I would. It is perfectly acceptable for an MXML Component to implement an interface. And it is perfectly acceptable to have multiple components implement the same interface to achieve different results.


For the sake of completeness, an MXML Component can implement an interface just like an ActionScript component ca:

<mx:myComp implements="com.myClass.Interface">
www.Flextras.com
A: 

You are correct, there is no way to implement a true interface using MXML(edit: I stand corrected, you can use the "implements" keyword as described in the other answers.) Another approach to consider is to use a "code behind" actionscript file for each of your 4 MXML files:

MXML file (MyFancyVBox.mxml):

<?xml version="1.0" encoding="utf-8"?>
<MyFancyVBoxCode>
...
</MyFancyVBoxCode>

AS file (MyFancyVBoxCode.as):

package com.something.whatever
{
    import com.something.another.IFancyInterface;

    public class MyFancyVBoxCode implements IFancyInterface
    {
        ...
    }
}

The downside is that it doubles the number of source files.

Wade Mueller
Now that I've read Flextras answer I'm not sure I understood or answered your question. Are you talking about declaring an interface in MXML or implementing an interface in MXML?
Wade Mueller
I could be wrong too; I thought the OP was asking about how to create an interface in MXML
www.Flextras.com
I was asking firstly is it possible to declare an interface in MXML and it seems like the answer is no. I was also alluding to the best approach to implement an interface with custom mxml components. Sorry if my question was a bit incoherrent.
dubbeat
+4  A: 

You can use interfaces with MXML components this way:

// YourClass.mxml
<mx:HBox implements="IYourInterface">

is an MXML equivalent of

// YourClass.as
class YourClass extends HBox implements IYourInterface

But you still need to create the interface (in this example IYourInterface) in Actionscript.

Robert Bak
+5  A: 

MXML can implement an interface, like Robert Bak said, but it cannot define an interface.

John Isaacks
A: 

Firstly, I agree with Wade that Code behind may help you.

Secondly, I am thinking do you need the interface in you case. In your question, you want "4 different implementions of the same thing". How about using the "state" in the mxml. It may solve you problems.

michael
It was my novice understanding that states are pretty much for different UI's right? My 4 implementations are related to data retrival and parsing. Theyre like content provders.Are states still a viable candidate?
dubbeat
Yes, you are right that states is not a good candidate for your case. However, I will not put the data retrival and parsing in the mxml file, because mxml is good for visual design. I will put that things in the class and make it independent to the mxml.
michael