翻译: Flex Collection 事件和手动通知变化

本文介绍如何使用Collection事件监控集合的变化,并更新UI显示。详细解释了CollectionEvent、PropertyChangeEvent及FlexEvent的应用,包括如何响应不同类型的集合变化进行相应操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Collection事件和手动变化通知

集合用事件来表示集合发后了变化. 你能用这些事件监控变化以作相应的显示上的更新.
Collection事件: CollectionEvent, PropertyChangeEvent和FlexEvent.

  1. 当今集合发生变化时, 发布 CollectionEvevnt.COLLECTION_CHANGE.
  2. CollectionEvent.kind(CollectionEventKind类型)用来表示是什么变化类型,比如UPDATE.
  3. CollectionEvent.items(是一个Array类型), 如果是ADD和REMOVE kind 事件, 这个数组包含了被删除或是被增加的items.   对于UPDATE事件, items则是一个事件数组,数组成员全都是PropertyChangeEvent, 每个事件表示相应的item的update
  4. PropertyChangeEvent.kind(类型是PropertyChangeEventKind)表示是那个属性发生了变化.
  5. 当游标位置发生了变化, 视图游标(View cursor)发布一个事件.   type属性是FlexEvent.CURSOR_UPDATE

 

你可以用这些集合事件来监控集合变化以更新显示。例如,一个自定义控件用一个collection作为它的data provider, 你肯定想每次collection发生变化时控件能被自动更新以显示修改后的最新数据. 这时上面这些集合事件就能派上用场.

假设我们做一个汽车租赁的预定系统. 应用程序就能用COLLECTION_CHANGE 事件来监听 预定信息和汽车信息.为这个事件的侦听器函数起个名字叫 reservationsChanged,这个函数判断 Change事件的kind 字段以作以下的不同业务逻辑.

  • 如果kind是ADD,    遍历事件的items属性,调用一个函数更新预定信息的显示(一个显示所有预定时间的框)
  • 如果kind是REMOVE, 遍历事件的items属性, 调用一个函数将这些预定信息从框中删除
  • 如果kind是UPDATE, 遍历事件的items属性, 此时每个item是一个PropertyChangeEvent事件对象, 我们调用函数更新所有对应的预定信息.
  • 如果kind是RESET,  调用一个函数重置预定信息.

代码如下:

 

  
  
  1. private function reservationsChanged (event :CollectionEvent ) : void {
  2.  
  3. switch (event.kind ) {
  4. case CollectionEventKind. ADD :
  5. for ( var i : uint = 0; i < event.items. length; i ++ ) {
  6. updateReservationBox (Reservation (event.items [i ] ) );
  7. }
  8. break;
  9. case CollectionEventKind.REMOVE :
  10. for ( var i : uint = 0; i < event.items. length; i ++ ) {
  11. removeReservationBox (Reservation (event.items [i ] ) );
  12. }
  13. break;
  14. case CollectionEventKind.UPDATE :
  15. for ( var i : uint = 0; i < event.items. length; i ++ ) {
  16. if (event.items [i ] is PropertyChangeEvent ) {
  17. if (PropertyChangeEvent (event.items [i ] ) != null ) {
  18. updateReservationBox (Reservation (PropertyChangeEvent (
  19. event.items [i ] ). source ) );
  20. }
  21. }
  22. else if (event.items [i ] is Reservation ) {
  23. updateReservationBox (Reservation (event.items [i ] ) );
  24. }
  25. }
  26. break;
  27. case CollectionEventKind.RESET :
  28. refreshReservations ( );
  29. break;
  30. }
  31. }

 

 

大家知道, 没有实现IEventDispatcher接口的数据项是不可监控的, 而且Flash和Flex对象和其它的基本数据类型都没有实现这个接口. 因此当你修改了Array或是一个DisplayObject对象的属性时,你就必须调用itemUpdated()方法来手动更新集合视图, 这个方法将 已被修改的item对象 和 修改之前的item对象 一并作为参数.

当你必须用Array来直接作为控件的dataProvider时, 你也可以用itemUpdated()方法来手动通知collection发生了变化。Array直接作dataProvider时, 控件会将数据封装到一个collection对象, 但是这个collection封装对象在Array数据发生改变时是不知道的,所以必须用itemUpdated()手动通知。

注: 当你直接在一个collection中增加或删除子项时,或是通过ICollectionView, IList的方法来修改colletion时, 你完全没有必须调用itemUpdated().

大家知道, 当一个类,或是一个对象的声明上加[Bindable]时,需要类(或是对象的类)实现了IEventDispatcher接口。因为只有实现了IEventDispatcher接口才能发布事件(propertyChange事件).

当你在类的声明之上加[Bindable]时,这个类的任何属性在发生变化时,都会发布propertyChange事件; 如果你仅是在指定属性之上加[Bindable]标记时,那只有加了[Bindable] meta tag的这些属性才会发布propertyChange事件。

collection会侦听propertyChange事件. 假设你有一个 myCollection, 这个collection的item的类有一个[Bindable] myVariable的变量,那么 myCollection.getItemAt(0).myVariable="myText" 将会触发一个propertyChange事件(假定是没有必要使用itemUpdated()的情况).

所以最常见的itemUpdate用法是: 一个不能Bindable的类,或是无法实现IEventDispatcher接口的类的数据(属性)发生变化时来用通知collection.下面的例子展示在这样的情景时你就可以用itemUpdated()

假设你有一个你不能控制和再编辑的类:

public class ClassICantEdit {    public var field1:String;    public var field2:String;}

你还有一个ArrayCollection, 里面的item全都是 classICantEdit对象.

public var myCollection:ArrayCollection = new ArrayCollection();

你有如下一个DataGrid控件:

<mx:DataGrid dataProvider="{myCollection}"/>

当你象下面更改myCollection中的item的值时, DataGrid控件是不会自动更新的:

myCollection.getItemAt(0).field1="someOtherValue";

为更新DataGrid控件,你必须 itemUpdated()函数:

myCollection.itemUpdated(collectionOfThoseClasses.getItemAt(0));

禁止和启用自动更新

Collection还提供了enableAutoUpdate()和disableAutoUpdate()方法,这两个方法可以启用或是禁止数据发生变化时自动更新数据视图的功能。

collection的 disableAutoUpdate()方法会阻止基本数据改变事件被collection视图广播.同时还阻止,collection自身作为一个结果集被改变的事件.
当一个collection绑定为一个控件的dataProvider时, 用这个方法可以防止因collection多次变化而引起控件不必要的中间显示更新。
例如, DataGrid控件在item被选中时,就会调用disableAutoUpdate(), 当这个item不再被选中时再调用enableAutoUpdate(),这样可以当你正在编辑一个item时,不会因为这item是在一个排序的collection中而导致在屏幕上下乱跳.
下面的代码片断显示了怎样调用disableAutoUpdate(), enableAutoUpdate()

var obj:myObject = myCollection.getItemAt(0);
myCollection.disableAutoUpdate();
obj.prop1 = 'foo';
obj.prop2 = 'bar';
myCollection.enableAutoUpdate();

例子: 在DataGrid控件中修改数据

下面的例子你可以在DataGrid控件中增加, 删除, 修改数据

 

  
  
  1. <?xml version= "1.0" ?>
  2.  
  3. <!-- dpcontrols/ModifyDataGridData.mxml -->
  4.  
  5. <mx :Application xmlns :mx= "http://www.adobe.com/2006/mxml" width= "500"
  6.  
  7. height= "600" >
  8.  
  9.  
  10.  
  11. <mx :Script >
  12.  
  13. <! [CDATA [
  14.  
  15. import mx.events. *;
  16.  
  17. import mx.collections. *;
  18.  
  19.  
  20.  
  21. // Add event information to a log (displayed in the TextArea).
  22.  
  23. public function collectionEventHandler (event :CollectionEvent ) : void {
  24.  
  25. switch (event.kind ) {
  26.  
  27. case CollectionEventKind. ADD :
  28.  
  29. addLog ( "Item " + event.location + " added" );
  30.  
  31. break;
  32.  
  33. case CollectionEventKind.REMOVE :
  34.  
  35. addLog ( "Item " + event.location + " removed" );
  36.  
  37. break;
  38.  
  39. case CollectionEventKind.REPLACE :
  40.  
  41. addLog ( "Item " + event.location + " Replaced" );
  42.  
  43. break;
  44.  
  45. case CollectionEventKind.UPDATE :
  46.  
  47. addLog ( "Item updated" );
  48.  
  49. break;
  50.  
  51. }
  52.  
  53. }
  54.  
  55. // Helper function for adding information to the log.
  56.  
  57. public function addLog (str : String ) : void {
  58.  
  59. log. text += str + "/n";
  60.  
  61. }
  62.  
  63.  
  64.  
  65. // Add a person to the ArrayCollection.
  66.  
  67. public function addPerson ( ) : void {
  68.  
  69. ac.addItem ( {first :firstInput. text, last :lastInput. text,
  70.  
  71. email :emailInput. text } );
  72.  
  73. clearInputs ( );
  74.  
  75. }
  76.  
  77.  
  78.  
  79. // Remove a person from the ArrayCollection.
  80.  
  81. public function removePerson ( ) : void {
  82.  
  83. // Make sure an item is selected.
  84.  
  85. if (dg.selectedIndex >= 0 ) {
  86.  
  87. ac.removeItemAt (dg.selectedIndex );
  88.  
  89. }
  90.  
  91. }
  92.  
  93.  
  94.  
  95. // Update an existing person in the ArrayCollection.
  96.  
  97. public function updatePerson ( ) : void {
  98.  
  99. // Make sure an item is selected.
  100.  
  101. if (dg.selectedItem !== null ) {
  102.  
  103. ac.setItemAt ( {first :firstInput. text, last :lastInput. text,
  104.  
  105. email :emailInput. text }, dg.selectedIndex );
  106.  
  107. }
  108.  
  109. }
  110.  
  111.  
  112.  
  113. // The change event listener for the DataGrid.
  114.  
  115. // Clears the text input controls and updates them with the contents
  116.  
  117. // of the selected item.
  118.  
  119. public function dgChangeHandler ( ) : void {
  120.  
  121. clearInputs ( );
  122.  
  123. firstInput. text = dg.selectedItem.first;
  124.  
  125. lastInput. text = dg.selectedItem.last;
  126.  
  127. emailInput. text = dg.selectedItem.email;
  128.  
  129. }
  130.  
  131.  
  132.  
  133. // Clear the text from the input controls.
  134.  
  135. public function clearInputs ( ) : void {
  136.  
  137. firstInput. text = "";
  138.  
  139. lastInput. text = "";
  140.  
  141. emailInput. text = "";
  142.  
  143. }
  144.  
  145.  
  146.  
  147. // The labelFunction for the ComboBox;
  148.  
  149. // Puts first and last names in the ComboBox.
  150.  
  151. public function myLabelFunc (item : Object ) : String {
  152.  
  153. return item.first + " " + item.last;
  154.  
  155. }
  156.  
  157. ] ] >
  158.  
  159. </mx :Script >
  160.  
  161.  
  162.  
  163. <!-- The ArrayCollection used by the DataGrid and ComboBox. -->
  164.  
  165. <mx :ArrayCollection id= "ac"
  166.  
  167. collectionChange= "collectionEventHandler(event)" >
  168.  
  169. <mx :source >
  170.  
  171. <mx : Object first= "Matt" last= "Matthews" email= "matt@myco.com" />
  172.  
  173. <mx : Object first= "Sue" last= "Sanderson" email= "sue@myco.com" />
  174.  
  175. <mx : Object first= "Harry" last= "Harrison" email= "harry@myco.com" />
  176.  
  177. </mx :source >
  178.  
  179. </mx :ArrayCollection >
  180.  
  181.  
  182.  
  183. <mx :DataGrid width= "450" id= "dg" dataProvider= "{ac}"
  184.  
  185. change= "dgChangeHandler()" >
  186.  
  187. <mx :columns >
  188.  
  189. <mx :DataGridColumn dataField= "first" headerText= "First Name" />
  190.  
  191. <mx :DataGridColumn dataField= "last" headerText= "Last Name" />
  192.  
  193. <mx :DataGridColumn dataField= "email" headerText= "Email" />
  194.  
  195. </mx :columns >
  196.  
  197. </mx :DataGrid >
  198.  
  199.  
  200.  
  201. <!-- The ComboBox and DataGrid controls share an ArrayCollection as their
  202.  
  203. data provider.
  204.  
  205. The ComboBox control uses the labelFunction property to construct the
  206.  
  207. labels from the dataProvider fields. -->
  208.  
  209. <mx :ComboBox id= "cb" dataProvider= "{ac}" labelFunction= "myLabelFunc" />
  210.  
  211.  
  212.  
  213. <!-- Form for data to add or change in the ArrayCollection. -->
  214.  
  215. <mx :Form >
  216.  
  217. <mx :FormItem label= "First Name" >
  218.  
  219. <mx :TextInput id= "firstInput" />
  220.  
  221. </mx :FormItem >
  222.  
  223. <mx :FormItem label= "Last Name" >
  224.  
  225. <mx :TextInput id= "lastInput" />
  226.  
  227. </mx :FormItem >
  228.  
  229. <mx :FormItem label= "Email" >
  230.  
  231. <mx :TextInput id= "emailInput" />
  232.  
  233. </mx :FormItem >
  234.  
  235. </mx :Form >
  236.  
  237.  
  238.  
  239. <mx :HBox >
  240.  
  241. <!-- Buttons to initiate operations on the collection. -->
  242.  
  243. <mx :Button label= "Add New" click= "addPerson()" />
  244.  
  245. <mx :Button label= "Update Selected" click= "updatePerson()" />
  246.  
  247. <mx :Button label= "Remove Selected" click= "removePerson()" />
  248.  
  249. <!-- Clear the text input fields. -->
  250.  
  251. <mx :Button label= "Clear" click= "clearInputs()" />
  252.  
  253. </mx :HBox >
  254.  
  255.  
  256.  
  257. <!-- The application displays event information here -->
  258.  
  259. <mx :Label text= "Log" />
  260.  
  261. <mx :TextArea id= "log" width= "100" height= "100%" />
  262.  
  263. </mx :Application >

 

 

Flash显示效果请到 http://livedocs.adobe.com/flex/3/html/help.html?content=about_dataproviders_5.html 文章最后部分查看.

本文翻译: http://www.smithfox.com/?e=38

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值