flash event_model_notification_order

Notification order

Imagine that you are subscribed to a magazine, you want to receive it before anyone else when it comes out, then once you have received it the magazine can be sent out to the other people who are subscribed to it.

This scenario isn’t very realistic but it is a good metaphor to illustrate the concept of priority notifications in the ActionScript 3 event model.

In order to specify the precise notification order we can use the priority parameter of the addEventListener method, and we get the following signature:

addEventListener(type:
String, listener:Function, useCapture:Boolean = false,
priority:int = 0, useWeakReference:Boolean = false):void

By taking our previous example, we specify the priority parameter:

myButton.addEventListener ( MouseEvent.CLICK, clickButton1, false, 0);
myButton.addEventListener ( MouseEvent.CLICK, clickButton2, false, 1);
function clickButton1 ( pEvt:MouseEvent ):void
{
// display : A [object SimpleButton] : myButton
trace( "A " + pEvt.target + " : " + pEvt.target.name );
}
function clickButton2 ( pEvt:MouseEvent ):void
{
// display : B [object SimpleButton] : myButton
trace( "B " + pEvt.target + " : " + pEvt.target.name );
}

When the *myButton button has been clicked, the clickButton1 function is triggered after the *clickButton2 function, the following message is displayed:

B [object SimpleButton] : myButton
A [object SimpleButton] : myButton

A listener function saved with a priority of 0 will be executed after a listener function saved with a priority of 1. It is impossible to modify the priority of a listener once it is saved, to do this we would be obliged to delete the listener with the help of the *removeEventListener** method, then to resave it.

This idea of priority doesn’t exist in the official DOM3 implementation and has only been added to ActionScript 3 to give more flexibility. Even if this functionality might seem useful in certain situations, playing with the notification order in your code is seen as bad practice. The update of your code can be difficult and can make it hard for your application to run.








Adobe Training center


Mediabox Training Centre © 2000 - 2008 All rights reserved.
Adobe Authorized Training Centre. State convention under number 25 14 02167 14.
Mediabox : SARL au capital de 62.000€ - Activity number: 25 14 02167 14 - SIRET : 493 716 468 00027
MEDIABOX, 102 Avenue des Champs Elysées, 75008 PARIS - Tel. +33(0)2.31.91.96.89 - Fax. +33(0)2.72.68.56.42