まめ畑

ゆるゆると書いていきます

動的プロパティを使う際に注意すること

動的プロパティをdataProviderにセットする時にwarningが出て、少し悩んでしまったのでメモ。
動的プロパティについての理解不足だった。


以下のようなコードを書いたらwarningが出る。

//piyopiyoなどの値は変数。(動的に変わる)
[Bindable]
private var _data:ArrayCollection = new ArrayCollection([
	{"piyo":piyopiyo, "fuga":fugafuga},
	{"piyo":hogehoge, "fuga":puyopuyo}
]);

//このデータを
//<mx:DataGrid id="piyo" dataProvider="{_data}" >
//みたいに感じでdataProviderにセット
//itemRendererの中で「data.piyo」の様にアクセス出来る

以下のようなwarningが出る

warning: unable to bind to property 'piyo' on class 'Object' (class is not an IEventDispatcher)
warning: unable to bind to property 'fuga' on class 'Object' (class is not an IEventDispatcher)

原因は、動的プロパティは明示的にBindableにする必要があるみたい。
ようは、Objectのプロパティを自分でBindableとして定義しなさいということ。

面倒でも、格納するプロパティ達をBindableに設定したクラスを作成して、そのインスタンスを格納することでwarningは出なくなった。

package
{
    public class fugaData
    {
        [Bindable]
        public var piyo:String;
        
        [Bindable]
        public var fuga:String;
        
        public function statusData(piyo:String="", fuga:String="")
        {
            this.piyo = piyo;
            this.fuga = fuga;
        }

    }
}

これを使って

private var _data:ArrayCollection = new ArrayCollection([
	new fugaData("fuga", "piyo"),
	new fugaData("hoge", "puyo")
]);

のように書き換えて解決。