Flex 事件机制

事件是所有交互式应用的基础,人机对话需要通过事件机制来完成,Flex应用程序能够为用户提供绝佳的使用体验,就必须要有完善的事件机制作为各种交互的基础。Flex事件机制遵守W3C标准--Document Object Model(DOM) Level 3 Events Specification Version 1.0,具体请参考:http://www.w3.org/TR/DOM-Level-3-Events/

    1.事件响应过程

    和其他语言时间处理机制不同,除了应用传统监听者设计模式来注册事件的监听外,Flex事件还分为三阶段--Capturing、Targeting、Bubbling,来应对更复杂场景的事件响应需求,根据Flex官方文档,对于三者的描述分别为:

    About the capturing phase
    The first part of the event flow is called the capturing phase. This phase comprises all of the nodes from the root node to the parent of the target node. During this phase, Flash Player examines each node, starting with the root, to see if it has a listener registered to handle the event. If it does, Flash Player sets the appropriate values of the Event object and then calls that listener. Flash Player stops after it reaches the target node's parent and calls any listeners registered on the parent.

    About the targeting phase
    The second part of the event flow, the targeting phase, consists solely of the target node. Flash Player sets the appropriate values on the Event object, checks the target node for registered event listeners, and then calls those listeners.

    About the bubbling phase
    The third part of the event flow, the bubbling phase, comprises all of the nodes from the target node's parent to the root node. Starting with the target node's parent, Flash Player sets the appropriate values on the Event object and then calls event listeners on each of these nodes. Flash Player stops after calling any listeners on the root node.

用文档中的插图来描述整个过程:



    Capturing阶段处理的对象是Stage和Parent,Targeting处理的对象是Child1和Child2,Bubbling针对Parent和Stage,并且按照顺序进行,这样就可以在不同的阶段去处理不同的事情,设计出结构更好的事件代码。

    2.target和currentTarget

    target指向事件的发起者,或为事件的调度对象,由谁产生这一系列的事件,target就指向谁,currentTarget为当前正在处理事件的结点,感觉就像是事件的两种this指针,跟踪事件的发展过程。举个更具体的例子,技术总监有一项任务,交给技术经理完成,技术经理将该任务分配给具体的技术工程师,技术工程师在执行该任务时,target指向技术总监,crrentTarget指向该技术工程师。

    3.事件停止

    stopPropagation和stopImmediatePropagation是停止事件处理的两个方法,两者的区别有点类似于循环中的continue和break,前者允许当前结点对象所有事件执行完毕后,再停止事件向前发展,后者直接停止所有事件。

    4.系统事件

    Flex事件分为用户事件和系统事件,前者存在于预定义好的组件之中,后者有两个重要的方法:Initialize和CreationComplete,在组件创建时调用。

    Initialize
    Fires before determines appearance
    Can use to set appearance properties

    CreationComplete
    Fires after measurement and layout
    Can use if code relies on appearance
    Fires after objects are created and ready for use

转载于:https://www.cnblogs.com/dingyingsi/archive/2013/06/01/3111801.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值