NotificationCenter
| Kind of class: | class |
|---|---|
| Inherits from: | none |
| Author: | Arthur Clemens |
| Classpath: | org.asapframework.events.notificationcenter.NotificationCenter |
| File last modified: | Monday, 09 October 2006, 23:36:13 |
NotificationCenter provides a way for objects that don't know about each other to communicate.
It receives Notification objects and broadcasts them to all interested objects.This is an almost complete ActionScript implementation of Apple Cocoa's NSNotificationCenter.
Usage:
Add an observer:
When the notification "AdviceAccordionDidUpdateNotification" is posted, method
The receiving class (the 'this' in the example above) should implement the method handleAdviceAccordionDidUpdate:
In a class, let's say class A, the object will post a notification like this:
Unregister the observer from the notification with:
Timeline notifications
The whole idea is that the posting object does not have to know the receiving object. This can be useful for sending an event somewhere from a (nested) timeline.
For example we want to draw a background before playing an external movie. At the last frame of the background drawing, we put:
On naming
Apple uses a naming convention for notification names. Examples of notification names are:
NotificationCenter.getDefaultCenter().addObserver(this, "handleAdviceAccordionDidUpdate", "AdviceAccordionDidUpdateNotification");The 'this' object will now listen to the notifications with name "AdviceAccordionDidUpdateNotification". This notification can be sent by any object, also from the timeline (see below).
When the notification "AdviceAccordionDidUpdateNotification" is posted, method
handleAdviceAccordionDidUpdate is called (second argument).The receiving class (the 'this' in the example above) should implement the method handleAdviceAccordionDidUpdate:
private function handleAdviceAccordionDidUpdate (inNote:Notification) : Void { var notificationName:String = inNote.name; var notificationObject:Object = inNote.object; var productData:Object = inNote.data; // do something with productData ... }
In a class, let's say class A, the object will post a notification like this:
NotificationCenter.getDefaultCenter().post("AdviceAccordionDidUpdateNotification", null, prodData);In our example,
prodData is an object that will be used by the receiving class.Unregister the observer from the notification with:
NotificationCenter.getDefaultCenter().removeObserver(this, "AdviceAccordionDidUpdateNotification");
Timeline notifications
The whole idea is that the posting object does not have to know the receiving object. This can be useful for sending an event somewhere from a (nested) timeline.
For example we want to draw a background before playing an external movie. At the last frame of the background drawing, we put:
stop(); import org.asapframework.events.notificationcenter.NotificationCenter; NotificationCenter.getDefaultCenter().post("MovieBackgroundDidFinishNotification");And in our manager class we implement:
NotificationCenter.getDefaultCenter().addObserver(this, "handleMovieBackgroundDidFinishNotification", "MovieBackgroundDidFinishNotification");
On naming
Apple uses a naming convention for notification names. Examples of notification names are:
TaskDidTerminateNotificationSo the formula is:
MenuWillFoldNotification
Class of Affected Object + Did/Will + Action + Notification
To do:
Find out if this can be used for Key-value observing similar to Cocoa's Registering Dependent Keys.
Summary
Constructor
Class methods
Instance methods
Constructor
NotificationCenter
function NotificationCenter (
inShouldNotifyErrors:Boolean)
Creates a new NotificationCenter. Call this constructor only if you explicitely don't want to use the default NotificationCenter, for instance if you want to control performance.
Parameters:
inShouldNotifyErrors:
(optional) if true, the NotificationCenter will report errors to the org.asapframework.util.debug.Console; by default error reporting is off.
See also:
Class methods
getDefaultCenter
static function getDefaultCenter (
Accesses the default notification center. For most cases you can just use this default notification center. If performance becomes problematic (if you have a few thousand observers, and need to do frequent adding and removing) it makes sense to instantiate a custom NotificationCenter object.
Returns:
Reference to the static NotificationCenter (Singleton).
Instance methods
addObserver
function addObserver (
inObserver:Object,
inMethodName:String,
inNotificationName:String,
inNotificationObject:Object) : Void
Registers inObserver to receive notifications with the name inNotificationName and/or containing inNotificationObject.
When a notification of name inNotificationName containing the object inNotificationObject is posted, inObserver's method inMethodName is called with a Notification as the argument. If inNotificationName is undefined, the notification center notifies the observer of all notifications with an object matching inNotificationObject. If inNotificationObject is nil, the notification center notifies the observer of all notifications with the name inNotificationName. inObserver may not be undefined.
When a notification of name inNotificationName containing the object inNotificationObject is posted, inObserver's method inMethodName is called with a Notification as the argument. If inNotificationName is undefined, the notification center notifies the observer of all notifications with an object matching inNotificationObject. If inNotificationObject is nil, the notification center notifies the observer of all notifications with the name inNotificationName. inObserver may not be undefined.
Parameters:
inObserver :
object to receive notifications
inMethodName :
The observer's method that will be called when sent a notification. This method should only have one argument (of type Notification).
inNotificationName :
(optional) notification identifier name; if undefined, you must use inNotificationObject
inNotificationObject:
(optional) notification identifier object; the notification center notifies the observer of all notifications with an object matching this object
Example:
This example adds an observer 'this', to let method 'doSomething' be called as soon as the notification named 'PanelWillUpdateNotification' is posted:
In the following example, we don't specify a notification name:
NotificationCenter.getDefaultCenter().addObserver(this, "doSomething", "PanelWillUpdateNotification");
In the following example, we don't specify a notification name:
NotificationCenter.getDefaultCenter().addObserver(this, "updatePanel", null, myPanel);Now all notifications with 'myPanel' as object argument will be passed (we'll pass the current time as data):
NotificationCenter.getDefaultCenter().post(null, myPanel, getTimer());
checkOnAdding
function checkOnAdding (
inFlag:Boolean) : Void
Set checking for doubles when calling addObserver. If true, each entry is checked if it is already in the list. For performance, checking is off by default.
Parameters:
inFlag:
When true, newly added observers will be checked if they are already in the list.
post
function post (
inNotificationName:String,
inNotificationObject:Object,
inData:Object) : Void
Creates a Notification instance and passes this to the observers associated through inNotificationName or inNotificationObject.
Parameters:
inNotificationName :
(optional) notification identifier name; if undefined, you must use inNotificationObject
inNotificationObject:
(optional) notification identifier object; typically the object posting the notification; may be null; if not null, any message is sent that is directed to this object
inData :
(optional) object to pass - this will be packed in the Notification
Example:
This example finds all observers that are associated with the notification name 'ButtonDidUpdateNotification', and passes them a Notification object with data "My message".
NotificationCenter.getDefaultCenter().post("ButtonDidUpdateNotification", null, "My message");The following example sends a notification to the observers that are associated with identifier object anIdentifier. Note that the name of the notification is not important when you pass an object - it may be an empty string. The object is associated with the observer in addObserver (the notification center notifies the observer of all notifications with an object matching inNotificationObject).
var anIdentifier:Object = this; NotificationCenter.getDefaultCenter().post(null, anIdentifier, "My message");
removeObserver
function removeObserver (
inObserver:Object,
inNotificationName:String,
inNotificationObject:Object) : Void
Removes inObserver as the observer of notifications with the name inNotificationName and object inNotificationObject from the object. inObserver may not be nil. Be sure to invoke this method before removing the observer object or any object specified in addObserver.
If inNotificationName is nil, inObserver is removed as an observer of all notifications containing inNotificationObject. If inNotificationObject is nil, inObserver is removed as an observer of inNotificationName containing any object.
If inNotificationName is nil, inObserver is removed as an observer of all notifications containing inNotificationObject. If inNotificationObject is nil, inObserver is removed as an observer of inNotificationName containing any object.
Parameters:
inObserver :
observing object
inNotificationName :
(optional) notification identifier name; if undefined, you must use inNotificationObject
inNotificationObject:
(optional) notification identifier object; specify when the observer listens to notifications with an object matching this object
Example:
To unregister someObserver from all notifications it had previously registered for, you would send this method:
NotificationCenter.getDefaultCenter().removeObserver(someObserver);To unregister the observer from the particular notification theNotificationName, use:
NotificationCenter.getDefaultCenter().removeObserver(someObserver, theNotificationName);
setNotifyErrors
function setNotifyErrors (
inFlag:Boolean) : Void
Parameters:
inFlag:
If true, NotificationCenter will report errors to the org.asapframework.util.debug.Console. Error reporting is off by default.
toString
function toString (
) : String