apache-flexmxml

Mandatory attributes in MXML?


Is it possible, through some kind of metadata presumably, to force a property of an object to be set? We have a number of non-visual components that simply doesn't do anything unless one or more properties are set, such as:

<ToolTip target="{this}">
    <mx:Image source="foo.png" />
</ToolTip>

In this case, the target property would be nice to mark as required, as it doesn't make sense to never have it set. This is not a huge issue since it's easy to document, but it would be nice to eliminate at least a few debug-roundtrips by having the compiler tell the developer of his error.

We don't wish for this tag to be redundant in any way, which could have been solved (in this instance) by simply making the ToolTip component a UIComponent and using the parent property. But for one thing, this adds unnecessary overhead and in other cases it's simply not proper:

<Button id="btn" label="Foo" />
<ToolTip target="{btn}">
    <mx:Image source="foo.png" />
</ToolTip>

So, are mandatory mxml attributes a possibility?


Solution

  • There's a solution, but it's not QUITE as simple as using metadata. Just have your non-visual components implement the IMXMLObject interface. The interface has just one method, "initialized". Implementing the interface allows you to inspect the object as its initialized via MXML.

    Thus...

    public function initialized(document:Object, id:String):void
    {
      if ( target == null ) throw new Error( "You must supply an argument to target!" );
    } 
    

    It'd be nice to have compile time checking, but this works decently.