Use complex data Object (with some nesting levels) piece in custom class by "address" - separated with dots inheritance description.
Parse obtained address and unfold data in for each loop.
Let's imagine situation when we obtain some complex Object as class data and can't change backend to pass exactly value we need. The only thing we can add custom fiels with needed data "address".
So, we have custom class nested from Text:
package custom
{
import mx.controls.Text;
public class Text extends mx.controls.Text {
private var _data:Object;
private var _labelField:String;
public function Text() {
super();
}
public function set customLabelField(labelField:String):void {
this._labelField = labelField;
}
public function get customLabelField():String {
return this._labelField;
}
}
}
used in application like
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
xmlns:custom="custom.*"
creationComplete="setText();">
<mx:Script>
<![CDATA[
private function setText():void{
var obj:Object = {piatto: {nome: 'testName'}}
testText.data = obj;
}
]]>
</mx:Script>
<custom:Text id="testText" customLabelField="piatto.nome" />
</mx:Application>
Let's override data field getter/setter in our custom class:
override public function set data(data:Object):void {
this._data = data;
for each(var token:String in _labelField.split(".")) data = data[token];
this.text = data.toString();
}
override public function get data():Object {
return this._data;
}
So, now we can reach neede data piece simply setting customLabelField as string description separated with dots.