艺虎动画 > flash动画制作中问题:Flex数据绑定陷阱:常见的误用和错误

flash动画制作中问题:Flex数据绑定陷阱:常见的误用和错误

翼虎动漫   2010-7-19

 

 

 

Using binding in place of events
在该使用事件的地方使用绑定

There are many cases where you can write your code easily without data binding and instead use events to assign a value.
You can set a value using the appropriate component lifecycle event or
overriding component’s methods such as childrenCreated() or initializationComplete() to do the a value assignment.
Additionally, there are times when you can use ChangeWatcher to listen to changes in data, which is less costly.
When using ChangeWatcher keep in mind that there are many cases where you can even avoid using the ChangeWatcher since you can manually get notification.
For instance, all collections in Flex have a collectionChange event broadcasted,
which you can use to manualy get change notification within the collection object.

有许多情况下,您可以轻松地写你的代码不用数据绑定,而是使用事件来赋值。
您可以使用适当的组件的生命周期事件设置一个值或重写组件的方法,如childrenCreated()或initializationComplete()来做一个赋值。
此外,有些时候你可以使用ChangeWatcher来监听数据的变化,这是一种成本比较低的做法。
当使用ChangeWatcher时,记住,有很多情况下,你甚至可以尽量避免使用ChangeWatcher,因为你可以手动的得到通知。
例如,在Flex中所有集合对象有一个collectionChange事件被广播,因此在集合对象中你可以手动获取改变的通知来使用。

Consider the following example:
思考下面的例子:

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
   xmlns:s="library://ns.adobe.com/flex/spark"
   xmlns:mx="library://ns.adobe.com/flex/mx"
   minWidth="1024" minHeight="768">
                        
   <fx:Script>
      <![CDATA[
         import mx.events.FlexEvent;
                                  
         [Bindable]
         public var dp:Array = [ { label:"New York", data: "New York" },
            { label:"Miami Beach", data: "Miami Beach" } ];
                                  
      ]]>
   </fx:Script>
                        
   <mx:ComboBox id="cb" editable="false" width="100" dataProvider="{dp}" />
                        
</s:Application>


It looks like pretty standard code for a Flex application.
However, the type of binding illustrated here is not needed.
Instead of binding you can use direct assignment in an event handler:

它看起来像一个漂亮的标准的Flex应用程序代码。
然而,这里这种类型的绑定是不必要的。
你可以在一个事件处理中直接赋值而不是使用绑定:

<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
   xmlns:s="library://ns.adobe.com/flex/spark"
   xmlns:mx="library://ns.adobe.com/flex/mx"
   minWidth="1024" minHeight="768"
   creationComplete="creationCompleteHandler(event)">
                        
   <fx:Script>
      <![CDATA[
         import mx.events.FlexEvent;
                                  
         public var dp:Array = [ { label:"New York", data: "New York" },
         { label:"Miami Beach", data: "Miami Beach" } ];
                                  
         protected function creationCompleteHandler(event:FlexEvent):void
         {
            cb.dataProvider = dp;
         }
                                  
      ]]>
   </fx:Script>
                        
   <mx:ComboBox id="cb" editable="false" width="100" />
                        
</s:Application>