views:

202

answers:

1

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?

+1  A: 

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.

jmreidy
Thanks a lot for your answer! I had no clue about that interface but I'm glad to hear about it. We have several classes that can benefit from this.It's unfortunate that compile time checking is out, but we'll definately manage with this interface. Thanks a lot!
macke
Follow up question: Is there another interface one can implement to learn when the property that the mxmlc compiler generates is changed? Let's say I have a non visual component that implements IMXMObject, and that the tag when added to an mxml document gets the id of "foo". This generates the property foo with a reference to the object instantiated from the mxml description, right? Now, if I change that reference to a new instance, can my instance somehow know about this? Also, I presume this interface is only good for declared instances, not runtime instances?
macke