转自:riameeting

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

大家知道, 没有实现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. <!-- dpcontrols/ModifyDataGridData.mxml -->
  3. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" width="500"
  4. height="600" >
  5. <mx:Script>
  6. <![CDATA[
  7. import mx.events.*;
  8. import mx.collections.*;
  9. // Add event information to a log (displayed in the TextArea).
  10. public function collectionEventHandler(event:CollectionEvent):void {
  11. switch(event.kind) {
  12. case CollectionEventKind.ADD:
  13. addLog("Item "+ event.location + " added");
  14. break;
  15. case CollectionEventKind.REMOVE:
  16. addLog("Item "+ event.location + " removed");
  17. break;
  18. case CollectionEventKind.REPLACE:
  19. addLog("Item "+ event.location + " Replaced");
  20. break;
  21. case CollectionEventKind.UPDATE:
  22. addLog("Item updated");
  23. break;
  24. }
  25. }
  26. // Helper function for adding information to the log.
  27. public function addLog(str:String):void {
  28. log.text += str + "/n";
  29. }
  30. // Add a person to the ArrayCollection.
  31. public function addPerson():void {
  32. ac.addItem({first:firstInput.text, last:lastInput.text,
  33. email:emailInput.text});
  34. clearInputs();
  35. }
  36. // Remove a person from the ArrayCollection.
  37. public function removePerson():void {
  38. // Make sure an item is selected.
  39. if (dg.selectedIndex >= 0) {
  40. ac.removeItemAt(dg.selectedIndex);
  41. }
  42. }
  43. // Update an existing person in the ArrayCollection.
  44. public function updatePerson():void {
  45. // Make sure an item is selected.
  46. if (dg.selectedItem !== null) {
  47. ac.setItemAt({first:firstInput.text, last:lastInput.text,
  48. email:emailInput.text}, dg.selectedIndex);
  49. }
  50. }
  51. // The change event listener for the DataGrid.
  52. // Clears the text input controls and updates them with the contents
  53. // of the selected item.
  54. public function dgChangeHandler():void {
  55. clearInputs();
  56. firstInput.text = dg.selectedItem.first;
  57. lastInput.text = dg.selectedItem.last;
  58. emailInput.text = dg.selectedItem.email;
  59. }
  60. // Clear the text from the input controls.
  61. public function clearInputs():void {
  62. firstInput.text = "";
  63. lastInput.text = "";
  64. emailInput.text = "";
  65. }
  66. // The labelFunction for the ComboBox;
  67. // Puts first and last names in the ComboBox.
  68. public function myLabelFunc(item:Object):String {
  69. return item.first + " " + item.last;
  70. }
  71. ]]>
  72. </mx:Script>
  73. <!-- The ArrayCollection used by the DataGrid and ComboBox. -->
  74. <mx:ArrayCollection id="ac"
  75. collectionChange="collectionEventHandler(event)">
  76. <mx:source>
  77. <mx:Object first="Matt" last="Matthews" email="matt@myco.com"/>
  78. <mx:Object first="Sue" last="Sanderson" email="sue@myco.com"/>
  79. <mx:Object first="Harry" last="Harrison" email="harry@myco.com"/>
  80. </mx:source>
  81. </mx:ArrayCollection>
  82. <mx:DataGrid width="450" id="dg" dataProvider="{ac}"
  83. change="dgChangeHandler()">
  84. <mx:columns>
  85. <mx:DataGridColumn dataField="first" headerText="First Name"/>
  86. <mx:DataGridColumn dataField="last" headerText="Last Name"/>
  87. <mx:DataGridColumn dataField="email" headerText="Email"/>
  88. </mx:columns>
  89. </mx:DataGrid>
  90. <!-- The ComboBox and DataGrid controls share an ArrayCollection as their
  91. data provider.
  92. The ComboBox control uses the labelFunction property to construct the
  93. labels from the dataProvider fields. -->
  94. <mx:ComboBox id="cb" dataProvider="{ac}" labelFunction="myLabelFunc"/>
  95. <!-- Form for data to add or change in the ArrayCollection. -->
  96. <mx:Form>
  97. <mx:FormItem label="First Name">
  98. <mx:TextInput id="firstInput"/>
  99. </mx:FormItem>
  100. <mx:FormItem label="Last Name">
  101. <mx:TextInput id="lastInput"/>
  102. </mx:FormItem>
  103. <mx:FormItem label="Email">
  104. <mx:TextInput id="emailInput"/>
  105. </mx:FormItem>
  106. </mx:Form>
  107. <mx:HBox>
  108. <!-- Buttons to initiate operations on the collection. -->
  109. <mx:Button label="Add New" click="addPerson()"/>
  110. <mx:Button label="Update Selected" click="updatePerson()"/>
  111. <mx:Button label="Remove Selected" click="removePerson()"/>
  112. <!-- Clear the text input fields. -->
  113. <mx:Button label="Clear" click="clearInputs()"/>
  114. </mx:HBox>
  115. <!-- The application displays event information here -->
  116. <mx:Label text="Log"/>
  117. <mx:TextArea id="log" width="100" height="100%"/>
  118. </mx:Application>

翻译: Flex Collection 事件和手动通知变化相关推荐

  1. Windwos下的互斥锁,事件以及事件的手动重置和自动重置的区别

    事件的自动重置:两个线层同时访问一个函数,该函数会对全局变量进行操作,用事件进行互斥,假设这个事件初始化有信号   在经过WAITFORSIGALOBJECT()后事件就会变为没有信号,另外一个线程无 ...

  2. SharePoint 2010/SharePoint 2013 Custom Action: 基于Site Collection 滚动文字的通知.

    应用场景: 有时候我们的站点需要在每个页面实现滚动文字的通知,怎么在不修改Master Page的情况下实现这个功能?我们可以使用Javascript 和 Custom Action 来实现. 创建一 ...

  3. Flex的事件(六)

    使用嵌套的函数作为事件监听器<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" ...

  4. Flex的事件(十四)

    一.   关于键盘事件<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /& ...

  5. 一些系统事件的手动触发方法

    测试UIViewController 触发UIViewController生命周期中的事件 调用 UIViewController.view, 它会触发 UIViewController.viewDi ...

  6. ant-design vue input通过那个事件可以获得输入框变化的值_VUE使用百度地图教程

    VUE使用百度地图教程 简介:传统的百度地图如果搭建在vuecli中就还需要再index.html的文件还需要配置script参数,这样使用起来就像是穿了一件不合身的衣服,总是难受,而且vue也不推荐 ...

  7. app功能性测试——安装卸载升级测试、交叉事件测试、通知推送测试

    安装卸载升级测试 1.安装 1) 软件安装各个选项的组合是否符合概要设计说明. 2) 软件安装向导的UI测试 3) 安装后没有生成多余的目录结构和文件. 4) 安装成功的情况(检查版本号是否正确)和安 ...

  8. C#自定义事件,监视变量变化

    首先监视定义类 class Event{public delegate void tempChange(object sender, EventArgs e);public event tempCha ...

  9. Flex启动事件的执行顺序

    很多时候,在创建一个Flex应用程序,不知道什么时候执行哪儿个启动事件.它们被执行的顺序是怎么排序的? 首先看一看都有哪儿些启动事件,主要有preinitialize,initialize,creat ...

最新文章

  1. Python游戏开发,Pygame模块,Python从零开始带大家实现一个魔塔小游戏
  2. python循环输出00-59
  3. 最全面的几何画板实用教程视频免费下载
  4. oracle 查询结果升序,Oracle学习日志-8(查询结果排序)
  5. Java LinkedList公共布尔提供(对象o)方法(带示例)
  6. 如何全面认识大数据分析的基础知识
  7. 手稿生成 HTML 代码 SketchCode
  8. 开启Ubuntu root 远程登录
  9. POCO c++ 使用例子
  10. B样条曲线(B-spline Curves)
  11. Qt设计师的简单使用(ui设计界面的简单使用)
  12. 面对众多的选择,我选择什么
  13. 程序猿生存指南-63 贪心姑娘
  14. selenium 定位一组元素
  15. 新华三联合英特尔共同增强服务器内存的可靠性
  16. Rockchip 红外遥控开发指南
  17. Web24——Ajax
  18. 安全可靠快速地导出微信聊天记录
  19. 7年iOS开发经验,教你写一份脱颖而出的简历,进入大厂机会翻3倍!
  20. Python ❀ 函数

热门文章

  1. Power BI Desktop中的“提问”功能
  2. 如何在Windows Server 2016上配置iSCSI启动器
  3. SQL Server中的登录触发器概述
  4. javascript中的Date类型
  5. 小甲鱼Python第十九讲课后习题
  6. Android 获取地理位置信息 封装好了 直接用
  7. 《疯狂Java讲义》(三十)---- Annotation
  8. Jquery垂直下拉二级菜单
  9. 简单的LRU Cache设计与实现
  10. 【记录】PS立体透明字效果