While working on a pureMVC project, I realised that there were N number of proxies registered with facade (yes, ApplicationFacade). The truth was, not all have been used during life cycle of a typical user functionality.
So what? It was merely difficult to categories the proxies for need of individual module/functionality.
The simple idea is to implement Lazy Loading / Lazy registration of proxy!
Instead of declaring statement at compile time like
facade.registerProxy(new MyProxy());
I overridden
retrieveProxy(...)
function in extended Facade class:
override public function retrieveProxy(proxyName:String):IProxy { var proxyInstance:IProxy = super.retrieveProxy(proxyName); if(proxyInstance==null) { var myClass:Class = getDefinitionByName(proxyName) as Class; if(!myClass) return null; var proxyInstance:IProxy = new myClass(); if(proxyInstance) registerProxy(proxyInstance); } return proxyInstance; }
This also takes care of not registering duplicate proxy.
Uh oh! While doing this trick, I encountered on more problem.
ReferenceError: Error #1065: Variable myClass is not defined.
I banged my head and tried to figure out the reason. After digging for some time, I found that the (class) definition of proxy was somehow not been retrieved with
getDefinitionByName()
.
It wasn’t possible to simply add import statements with those Proxy class names as Flex compiler ignores such unused (yes while compiling none of proxy was referenced) imports.
Simple trick worked:
in extended facade class constructer, I declared each proxy as variable, something like:
public class ApplicationFacade extends Facade { public function ApplicationFacade(parameter:SingletonEnforcer) { super(); var myProxy:MyProxy; } }
and that’s all I wanted! The trick is this code doesn’t force an instance of proxy to be created, still at runtime when required, it finds definition of MyProxy class.