PureMVC Architects Lounge

Announcements and General Discussion => General Discussion => Topic started by: badoumba on November 09, 2011, 11:24:14



Title: About code structure
Post by: badoumba on November 09, 2011, 11:24:14
Very quick and simple question.

What's the best practice if I have a variable that will be used by many mediators and skins and that I don't want to duplicate (a Matrix value). Should I place it in the facade?

Thanks


Title: Re: About code structure
Post by: puremvc on November 10, 2011, 09:46:03
Don't store it on the Facade. That's the sort of thing that leads to Singletons having a bad reputation.

Instead, store it in a "transient Proxy". An instance of the framework Proxy class just used to hold an arbitrary value.

Inside a PrepareViewCommand (or the 'view prep phase' of StartupCommand)

:
// Store the Matrix
var matrix:Matrix = new Matrix();
var matrixProxy:Proxy = new Proxy( AppConstants.MATRIX, matrix );
// Register Mediators...

Inside each Mediator that needs a reference to this matrix :
:
protected var matrixProxy:IProxy;
override public function onRegister():void
{
   matrixProxy = facade.retrieveProxy(AppConstants.MATRIX);
   matrixNeedingComponent = matrixProxy.getData() as Matrix;
}

protected function get matrixNeedingComponent():MatrixNeedingComponent
{
   return viewComponent as MatrixNeedingComponent;
}
If there are a lot of these mediators, you might consider making an AbstractMediator they inherit from and call super.onRegister() from your subclasses to get the matrix proxy.


Inside a Command that needs to reference the Matrix

:
 var matrixProxy:IProxy = facade.retrieveProxy(AppConstants.MATRIX);
  var matrix:Matrix = matrixProxy.getData() as Matrix;

   // do stuff with the matrix