views:

125

answers:

1

Hello,

I have an actionscript class called Dimension, which allows a client to specify a dimension using a value and a unit such as "CM" or "Inches". I want to use an instance of this class as a property in MXML, so a user can write

<DimensionView value="2cm"/>

How do I make "2cm" an accepted string value for Dimension? I assume I need to write a parser method on my Dimension class, but I can't work out which interface I should be implementing to provide this functionality.

Can anyone help?

+1  A: 

One option is to just type the value property as a String, write a getter and a setter for it and do the parsing there:

/**
* docs here
*/
[Bindable(event="valueChanged")]
public function get value():String
{
    return _valueInt.toString();
}
/**
* @private
*/
public function set value(aVal:String):void
{
    // parse the aVal String to an int (or whatever) here
    _valueInt = parsed_aVal;
    dispatchEvent(new Event("valueChanged"));
}

On a related note, the framework components implement the feature of allowing for the usage of percentage signs in some sizing properties, when assigned in MXML, by using an undocumented metadata field called PercentProxy. The below example is the width property getter and setter from mx.core.UIComponent:

[Bindable("widthChanged")]
[Inspectable(category="General")]
[PercentProxy("percentWidth")]
override public function get width():Number
{
    // --snip snip--
}
override public function set width(value:Number):void
{
    // --snip snip--
}
hasseg