Rich Rodecker’s blog on flash, flex, actionscript, javascript, and php, with a dash of randomness
How I (sort of) modified Flex’s SystemManager.initialize() method
I'm not sure if this method is way off base or not, but it worked. In this case, I wanted to specify a different class to be returned when calling ResourceManager.getInstance(). Currently, flex apps are pretty much hardcoded to return an instance of the mx.resources.ResourceManagerImpl class.
How does it that method know to return an instance of that class? Well, the Flex framework uses a class called Singleton, which keeps a registry that maps interface names to classes that are supposed to be singletons in the app. So, ResourceManager.getInstance() calls Singleton.getInstance() to ask if for the IResourceManager class to use in the app:
-
instance = IResourceManager(Singleton.getInstance("mx.resources::IResourceManager"));
The problem I ran into is that for IResourceManager, and a few other interfaces, the class to return is registered in the SystemManager.initialize() method, which runs way before any of the actual flex application code is available.
However, there is one custom class which can be accessed during the SystemManager.intialize() method: the custom preloader class. The preloader class is accessed a few lines above where Singleton.registerClass() is called to register the IResourceManager singleton.
So, I made a subclass of DownloadProgressBar and set that to be the preloader class. That class uses a static initializer to register my own class to use for ResourceManager.getInstance():
-
private static var classConstructed:Boolean = classConstruct();
-
-
private static function classConstruct():Boolean {
-
var rm:ResourceManager2;
-
Singleton.registerClass("mx.resources::IResourceManager",
-
Class(getDefinitionByName("com.venarc.designer.managers.ResourceManager2")));
-
return true;
-
}
Now my custom ResourceManager2 class is used whenever you call ResourceManager.getInstance(). I wish there were some compiler options for specifying which classes to use for those Singletons, would've made everything a lot easier.
about 8 months ago
Great idea. Note that in ResourceManager there is a try catch with a comment indicating that in non-Flex apps and modules, it won't work and seems to indicate that FlexModuleFactory may register the singleton as well. I can't quite tell if that means your custom class will get used or not in a module. I don't see how a FlexModuleFactory would ever register it before SystemManager.
about 8 months ago
Registering the singletons is a "first-come, first-serve" basis. It can't be overrriden once it is set. The comment you're mentioning is speaking about within the AS3 app or Module itself, if you try to access ResourceManager at all. I think that since with this, since you're registering the singleton in the SystemManager, it should still work within the loaded module. This is the next step I'm working on anyway, so I'll post an update whether it works or not.