我的容器对象的定义:
private static const emptyLink:Object = { id: -1,title:'',trigger1:'',trigger2:'',trigger3:'',trigger4:'',trigger5:'',linkTitle:'',linkBody:'',answer1:'',answer2:'',answer3:'',answer4:'',answer5:'' }; [Bindable] public var currentLink:Object = emptyLink;
currentLink在运行时被分配到ArrayCollection的特定索引,我主要使用emptyLink对象进行初始化。
<mx:Panel id="triggerPanel" title="Trigger" width="33%"> <mx:VBox id="tpBoxes" width="100%" paddingBottom="5" paddingLeft="5" paddingRight="5" paddingTop="5"> <mx:TextInput id="trigger1" width="100%" textAlign="left" text="{currentLink.trigger1}" /> <mx:TextInput id="trigger2" width="100%" textAlign="left" text="{currentLink.trigger2}" /> <mx:TextInput id="trigger3" width="100%" textAlign="left" text="{currentLink.trigger3}" /> <mx:TextInput id="trigger4" width="100%" textAlign="left" text="{currentLink.trigger4}" /> <mx:TextInput id="trigger5" width="100%" textAlign="left" text="{currentLink.trigger5}" /> </mx:VBox> </mx:Panel>
当然,这个编译和显示很好,但每个实例都有运行时警告:
warning: unable to bind to property ‘trigger1’ on class ‘Object’ (class is not an IEventDispatcher)
warning: unable to bind to property ‘trigger2’ on class ‘Object’ (class is not an IEventDispatcher)
warning: unable to bind to property ‘trigger3’ on class ‘Object’ (class is not an IEventDispatcher)
warning: unable to bind to property ‘trigger4’ on class ‘Object’ (class is not an IEventDispatcher)
warning: unable to bind to property ‘trigger5’ on class ‘Object’ (class is not an IEventDispatcher)
当TextInput字段更改时,currentLink对象不会更新。
明显的答案是我的对象需要一个实现IEventDispatcher的类的实例。这个答案没有告诉我的是,实现该接口(需要什么,什么不是)的细节,如果有一个更简单的方法来做 – 像一个内置的类,将乐意接受我的自定义属性,并允许对于绑定,没有我不必担心实现接口的细节。
这样的类是否存在?如果没有,完成这项任务的最低和/或接受的标准是什么?
解决方法
<?xml version="1.0" encoding="utf-8"?> <mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"> <mx:Script> <![CDATA[ import mx.utils.ObjectProxy; private static const emptyLink:Object = { id: -1,answer5:'' }; [Bindable] public var currentLink:ObjectProxy = new ObjectProxy(emptyLink); private function handleClick():void { trace(currentLink.trigger1); } ]]> </mx:Script> <mx:Panel id="triggerPanel" title="Trigger" width="33%"> <mx:VBox id="tpBoxes" width="100%" paddingBottom="5" paddingLeft="5" paddingRight="5" paddingTop="5"> <mx:TextInput id="trigger1" width="100%" textAlign="left" text="{currentLink.trigger1}" valueCommit="{currentLink.trigger1 = trigger1.text;}"/> <mx:Button label="Click" click="handleClick()"/> </mx:VBox> </mx:Panel> </mx:WindowedApplication>