Learning Flex - Event

Actually I started the learning of Flex a couple of days ago and made notes as I used to learn other new techniques. Today I suddenly realize that I should write in down onto my BLOG. That's not only for my reference later but also for share. So I posted part of my notes about Event as kick-off :-)


Chapter 4 Using Events

 

1. About Events

 

2. Using Events

 

It comprises two steps:

(1) Write a function or class method as event listener.

(2) Register it as listener to an event.

 

We can register event handler in two ways

(1) Use “addEventListener()” in ActionScript blocks.

(2) Add event listener inline (MXML)

 

"addEventListener” is preferred because we can “removeEventListener” later, while we cannot using inline way. What’s more, you can configure priority, capturing settings and use event constants.

 

The event object argument of an event handler function (or class method) is optional.

 

Event objects include a reference to the instance of the dispatching component (or target), “currentTarget”. While accessing methods and properties of it, the best practice is to cast currentTarget to whatever class you anticipate will dispatch this event. Otherwise you will get runtime error instead of compile error.

 

Mostly often we pass the entire event object to a listener, but we can just pass individual properties of the event object, too.

 

addEventListener(

    event_type:String,           // required

    event_listener:Function,   // required

    use_capture:Boolean,      // optional

    priority:int,                      // optional

    weakRef:Boolean            // optional

);

 

event_type

We can use string like “click”, “mouseOut” or static constant like MouseEvent.CLICK, MouseEvent.MOUSE_OUT. Adobe recommends static constant as it brings compile-time checking, while misspell string of event_type will cause unexpected behavior.

 

event_listener

Function handles the event. It must declare at least an argument of event.

 

use_capture

It sets useCapture property of the Event object. Event handler will be active only in capturing phase if useCapture is “true”, or only in targeting and bubbling phase if useCapture is “false”.

 

priority

It sets priority of that event listener. The higher the value is, the sooner the event listener will be executed relative to other listeners for the same event. Listeners with the same priority value will be executed in the order they were added.

 

weakRef

The default value “false” prevents listener from being garbage collected.

 

To Flex, an inner function is actually an object, and can be freed by the garbage collector. So if the listener passed to addEventListener() is an inner function, we shouldn’t pass “true” for weakRef argument. That will possibly cause event listener to be freed by event comes and consequently unexpected results.

 

removeEventListener(

    event_type:String,            // required

    event_listener:Function,    // required

    use_capture:Boolean,       // optional

);

 

Creating event handler class

Objects themselves cannot be event listener, but method of an object can. Adobe recommends defining one external class that handles all event listeners. That makes MXML application more readable and maintainable.

 

If we want to use a non-static class method as listener, we must instantiate an object of that class. A static class method can be registered as listener directly.


 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值