[Flex] Load custom configuration content before Application is initialized
5285 단어 application
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.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Pre-Query SamplesValidate the current query criteria or provide additional query criteria programmatically, just before sending the SELEC...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.