/*
PureMVC - Copyright(c) 2006-08 Futurescale, Inc., Some rights reserved.
Your reuse is governed by the Creative Commons Attribution 3.0 United States License
*/
package org.puremvc.as3.core
{
import org.puremvc.as3.interfaces.*;
import org.puremvc.as3.patterns.observer.Observer;
/**
* A Singleton IView
implementation.
*
*
* In PureMVC, the View
class assumes these responsibilities:
*
IMediator
instances.IMediators
.IMediators
when they are registered or removed.INotification
in the application.IObservers
to an INotification
's observer list.INotification
.IObservers
of a given INotification
when it broadcast.
* This IView
implementation is a Singleton,
* so you should not call the constructor
* directly, but instead call the static Singleton
* Factory method View.getInstance()
*
* @throws Error Error if Singleton instance has already been constructed
*
*/
public function View( )
{
if (instance != null) throw Error(SINGLETON_MSG);
instance = this;
mediatorMap = new Array();
observerMap = new Array();
initializeView();
}
/**
* Initialize the Singleton View instance.
*
*
* Called automatically by the constructor, this * is your opportunity to initialize the Singleton * instance in your subclass without overriding the * constructor.
* * @return void */ protected function initializeView( ) : void { } /** * View Singleton Factory method. * * @return the Singleton instance ofView
*/
public static function getInstance() : IView
{
if ( instance == null ) instance = new View( );
return instance;
}
/**
* Register an IObserver
to be notified
* of INotifications
with a given name.
*
* @param notificationName the name of the INotifications
to notify this IObserver
of
* @param observer the IObserver
to register
*/
public function registerObserver ( notificationName:String, observer:IObserver ) : void
{
var observers:Array = observerMap[ notificationName ];
if( observers ) {
observers.push( observer );
} else {
observerMap[ notificationName ] = [ observer ];
}
}
/**
* Notify the IObservers
for a particular INotification
.
*
*
* All previously attached IObservers
for this INotification
's
* list are notified and are passed a reference to the INotification
in
* the order in which they were registered.
INotification
to notify IObservers
of.
*/
public function notifyObservers( notification:INotification ) : void
{
if( observerMap[ notification.getName() ] != null ) {
// Get a reference to the observers list for this notification name
var observers_ref:Array = observerMap[ notification.getName() ] as Array;
// Copy observers from reference array to working array,
// since the reference array may change during the notification loop
var observers:Array = new Array();
var observer:IObserver;
for (var i:Number = 0; i < observers_ref.length; i++) {
observer = observers_ref[ i ] as IObserver;
observers.push( observer );
}
// Notify Observers from the working array
for (i = 0; i < observers.length; i++) {
observer = observers[ i ] as IObserver;
observer.notifyObserver( notification );
}
}
}
/**
* Remove the observer for a given notifyContext from an observer list for a given Notification name.
*
* @param notificationName which observer list to remove from
* @param notifyContext remove the observer with this object as its notifyContext
*/
public function removeObserver( notificationName:String, notifyContext:Object ):void
{
// the observer list for the notification under inspection
var observers:Array = observerMap[ notificationName ] as Array;
// find the observer for the notifyContext
for ( var i:int=0; i
* Registers the
* If the View
.
*
* IMediator
so that it can be retrieved by name,
* and further interrogates the IMediator
for its
* INotification
interests.IMediator
returns any INotification
* names to be notified about, an Observer
is created encapsulating
* the IMediator
instance's handleNotification
method
* and registering it as an Observer
for all INotifications
the
* IMediator
is interested in.IMediator
instance
* @param mediator a reference to the IMediator
instance
*/
public function registerMediator( mediator:IMediator ) : void
{
// do not allow re-registration (you must to removeMediator fist)
if ( mediatorMap[ mediator.getMediatorName() ] != null ) return;
// Register the Mediator for retrieval by name
mediatorMap[ mediator.getMediatorName() ] = mediator;
// Get Notification interests, if any.
var interests:Array = mediator.listNotificationInterests();
// Register Mediator as an observer for each of its notification interests
if ( interests.length > 0 )
{
// Create Observer referencing this mediator's handlNotification method
var observer:Observer = new Observer( mediator.handleNotification, mediator );
// Register Mediator as Observer for its list of Notification interests
for ( var i:Number=0; iView
.
*
* @param mediatorName the name of the IMediator
instance to retrieve.
* @return the IMediator
instance previously registered with the given mediatorName
.
*/
public function retrieveMediator( mediatorName:String ) : IMediator
{
return mediatorMap[ mediatorName ];
}
/**
* Remove an IMediator
from the View
.
*
* @param mediatorName name of the IMediator
instance to be removed.
* @return the IMediator
that was removed from the View
*/
public function removeMediator( mediatorName:String ) : IMediator
{
// Retrieve the named mediator
var mediator:IMediator = mediatorMap[ mediatorName ] as IMediator;
if ( mediator )
{
// for every notification this mediator is interested in...
var interests:Array = mediator.listNotificationInterests();
for ( var i:Number=0; i