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

If you have built a custom item renderer for a DataGroup or List you may have come across warnings in your Flash Builder console output similar to this:

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


This kind of warning typically happens when you are trying to bind to an object or property that isn’t defined as Bindable. I’ve seen this issue come up often when working with dataProviders that use <fx:Object />. For example, when running this application:

<s:Application 
    xmlns:fx="http://ns.adobe.com/mxml/2009" 
    xmlns:s="library://ns.adobe.com/flex/spark">
 
    <s:List>
        <s:dataProvider>
            <s:ArrayList>
                <fx:Object firstName="Steve" lastName="Yzerman" />
                <fx:Object firstName="Joe" lastName="Sakic" />
            </s:ArrayList>
        </s:dataProvider>
        <s:itemRenderer>
            <fx:Component>
                <s:ItemRenderer>
                    <s:HGroup>
                        <s:Label text="{data.firstName}" />
                        <s:Label text="{data.lastName}" />
                    </s:HGroup>
                </s:ItemRenderer>
            </fx:Component>
        </s:itemRenderer>
    </s:List>
 
</s:Application>

The Flash Builder console outputs:

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

The issue is this custom inline renderer uses the { } binding syntax to bind to the value of data.firstName and data.lastName, but since the properties aren’t defined as bindable, changes to the data won’t get updated in the renderer (hence the warning).

There are two ways of changing your application to avoid these warnings.

1. Change your item renderer

Instead of binding the Label’s text property to the data you can override the set data method on the item renderer and update the Labels from there:

<s:ItemRenderer>
    <fx:Script>
        <![CDATA[
            override public function set data(value:Object):void {
                txtFirstName.text = value.firstName;
                txtLastName.text = value.lastName;
            }
        ]]>
    </fx:Script>
    <s:HGroup>
        <s:Label id="txtFirstName" />
        <s:Label id="txtLastName" />
    </s:HGroup>
</s:ItemRenderer>

If you aren’t comfortable overriding the set data method you might find it easier to listen for the dataChange event, for example:

<s:ItemRenderer dataChange="updateRenderer()">
    <fx:Script>
        <![CDATA[
            public function updateRenderer():void {
                txtFirstName.text = data.firstName;
                txtLastName.text = data.lastName;
            }
        ]]>
    </fx:Script>
    <s:HGroup>
        <s:Label id="txtFirstName" />
        <s:Label id="txtLastName" />
    </s:HGroup>
</s:ItemRenderer>

Note: You should never key off of the creationComplete event in a spark item renderer unless you are fully aware of the consequences of item renderer recycling. Listening for the dataChange event is recommended.

2. Change the objects in your dataProvider

If you would like to take full advantage of the Binding syntax in your renderer then the objects in your dataProvider must be bindable. You can do this by creating a custom data type and defining some bindable properties on it. For example create a file called HockeyPlayer.as:

package
{
    public class HockeyPlayer extends Object
    {
        [Bindable] public var firstName:String;
        [Bindable] public var lastName:String;
    }
}

And then using this data structure in the dataProvider:

<s:Application 
    xmlns:fx="http://ns.adobe.com/mxml/2009" 
    xmlns:s="library://ns.adobe.com/flex/spark" 
    xmlns:local="*">
 
    <s:List>
        <s:dataProvider>
            <s:ArrayList>
                <local:HockeyPlayer firstName="Joe" lastName="Sakic" />
                <local:HockeyPlayer firstName="Steve" lastName="Yzerman" />
            </s:ArrayList>
        </s:dataProvider>
        <s:itemRenderer>
            <fx:Component>
                <s:ItemRenderer>
                    <s:HGroup>
                        <s:Label id="txtFirstName" text="{data.firstName}" />
                        <s:Label id="txtLastName" text="{data.lastName}" />
                    </s:HGroup>
                </s:ItemRenderer>
            </fx:Component>
        </s:itemRenderer>
    </s:List>
 
</s:Application>
from: http://flexponential.com/2009/11/11/binding-warnings-when-using-object-in-a-list-dataprovider/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值