I’ve seen a few people ask on the flexcoders mailing list about different methods of loading applications at run time based on a user’s login without navigating to a different URL. There are several ways of doing this using a ViewStack or view states to toggle between application views, using a SWFLoader to load in separate standalone SWF movies at runtime, or using a ModuleLoader to load in Flex modules at runtime, each based on a user’s access rights.
Using a ViewStack/states to accomplish this is probably the least desirable method, especially if your application is even mildly complex. In essence you’re just toggling which view is visible, but each view exists statically in the application at all times. This can cause your application’s file size to be quite large, and can lead to messy event registration and clean up and really complex view clean up logic.
Using a SWFLoader is a bit better with memory clean up and event management, but still isn’t the optimal solution as each of the modules that you download will generally be larger than they need to be because they’re standalone applications and include the overhead of classes already included in the loading application (frameworks, dependent libs, etc.). This will result in a smaller loading application, but when you factor in the size of each subsequent SWF loaded, the total application file size is substantially higher. It also can be a bit complex to manage the applications being loaded in.
Using a ModuleLoader (or ModuleManager) to load Flex modules into the application at run time is very similar to using a SWFLoader: you’re still loading SWF files into the application at runtime, but the difference is the size of the loaded files. Flex Modules are based on either mx:Module if it’s a component that interacts with the framework (view components generally) or mx:ModuleBase if it’s a component that doesn’t need to add things to the display list. This cuts out a lot of overhead as it doesn’t have to include all the application-related objects. Flex Builder also has a built-in tool to help define these modules for your project, making managing them very easy. (See Project Properties -> Flex Modules)
I put together a simplified example using this approach. It’s really just an example and doesn’t have much real-world implementation code, but it does show how to use runtime data to define your application’s behavior while keeping memory and download sizes at a minimum.
My approach involves using a component that consists of a ModuleLoader to load modules in by their package name (eg: “com.beauscott.examples.modules.Module1″), a global UserManager object that handles all login/logout event dispatching and related model information, and a series of 4 modules that are loaded per each users’ module access rights. Each user defined can have one or more modules permitted for their use. If only one is defined, the module loader automatically loads it. If more than one are defined, it will prompt you with a list of available modules to choose from (double-click).
It’s very rough and there may be better methods out there, but I hope this helps as a starting point. I’d love some suggestions and feedback as usual