[Flex] Load custom configuration content before Application is initialized

5285 단어 application
Programmers with certain development experience are accustomed to storing some configurable information content in a custom configuration file. When the configuration needs to be changed, only the configuration file needs to be modified, and there is no need to recompile and publish the program.
Under normal circumstances, it is not difficult to read custom configuration files in Flex, but sometimes it is required to obtain complete configuration information before Application initialization, so a little effort must be made.
Before the Application is initialized, only the preloader can operate. If the reading of the custom configuration file and the downloading of the class library by the preloader are regarded as the same processing, the function can basically be achieved.
 
Note: The preloader can only use the class libraries of flash and some mx namespaces. Do not use the mx and spark control class libraries, otherwise the work of the preloader will not be preloaded.
​​
Custom configuration information class:

public class Config
{
    public static const URL:String = "config.xml";

    private static var _instance:Config;

    public static function getInstance():Config
    {
        if (_instance == null)
            _instance = new Config();
        return _instance;
    }

    private var _config:XML;

    public function get config():XML
    {
        return _config;
    }

    public function set config(value:XML):void
    {
        _config = value;
    }	
}
 
Loading the custom configuration file is done after the preloader has downloaded all the class libraries and the application is initialized, so the PRELOADER_DOC_FRAME_READY event of the preloader is intercepted here.

public class SparkPreloader extends SparkDownloadProgressBar
{
    private var _preloader:Sprite;	
    private var suspend:Boolean;
    private var _urlLoader:URLLoader;

    public function SparkPreloader()
    {
        super();
    }

    override public function set preloader(value:Sprite):void
    {
        _preloader = value;
        super.preloader = value;
        value.addEventListener(FlexEvent.PRELOADER_DOC_FRAME_READY, preloaderDocFrameReadyHandler, false, int.MAX_VALUE); // 
    }

    private function preloaderDocFrameReadyHandler(event:FlexEvent):void
    {
        // Application
        event.stopImmediatePropagation();
        if (suspend)
            return;

        suspend = true; // 
        startConfiguation(); // 
    }
	
    private function startConfiguation():void
    {
        _urlLoader = new URLLoader();
        _urlLoader.addEventListener(Event.COMPLETE, completeHandler);
        _urlLoader.load(new URLRequest(Config.URL));
    }

    private function completeHandler(e:Event):void
    {
        _urlLoader.removeEventListener(Event.COMPLETE, completeHandler);

        Config.config = new XML(_urlLoader.data);

        _urlLoader = null;

        _preloader.removeEventListener(FlexEvent.PRELOADER_DOC_FRAME_READY, preloaderDocFrameReadyHandler); // 
        _preloader.dispatchEvent(new FlexEvent(FlexEvent.PRELOADER_DOC_FRAME_READY)); // Application
    }

}
Flex startup process reference "Analysis of the Flex startup process" Simply put, Flex is still swf in the final analysis. Like flash, it has a timeline and frames, but Flex usually only has two frames, the first frame is the preloader, and the second frame is the application. The loading interface we usually see is the preloader of the first frame. When the download is completed, it jumps to the second frame to initialize the application, and dispatches the PRELOADER_DOC_FRAME_READY event. In order to read the configuration content before the application is initialized, the PRELOADER_DOC_FRAME_READY event must be monitored, and it must be the highest level of monitoring, because the SystemManager performs the initialization of the second frame after monitoring the PRELOADER_DOC_FRAME_READY event, and it is difficult for us to modify the SystemManager. In the code, I borrowed the method in my "Achieving Sequential Execution of Modules in Flex" article. If there are multiple configurations that need to be read, or there are other initialization tasks, add them through addChild. Because the PRELOADER_DOC_FRAME_READY event is continuously dispatched, I added the suspend flag to prevent repeated processing. After the configuration reading is completed, remove the listener and actively dispatch the PRELOADER_DOC_FRAME_READY event to let the SystemManager continue to work. After testing, no errors were reported again. I think this should be the perfect solution.

좋은 웹페이지 즐겨찾기