| LowRA API Documentation | Annexes | All Packages | All Classes | Index | Frames |
|
| How to load context at runtime | ||
You will find below the method to load xml context at runtime.
Welcome to dynamic context for LowRA IoC.
Main application is loaded and so your main xml context too( main application context file, resources, and more... ).
Application has started, and all works well.
After user actions or at some point in your application, you need a new plugin ( and its context definition ), to add features to our application now; and only "at runtime". You don't want to include this plugin all the time in your main xml context.
Example of this possible context file ( monitorContext.xml ) which add the Kairos Monitor support application.
<beans>
<dll url="MonitoringPluginDLL.swf" />
<plugin id="Monitor" type="lowra.plugins.utils.MonitoringPlugin">
<property name="x" value="200" /> <property name="y" value="0" /> <property name="alpha" value="0.5" />
</plugin>
</beans>
To load context at runtime, use the com.bourre.ioc.load.runtime.RuntimeContextLoader class.
Example :
var rtLoader : RuntimeContextLoader = new RuntimeContextLoader( );
rtLoader.addEventListener( ApplicationLoaderEvent.onApplicationInitEVENT, onComplete );
rtLoader.load( new URLRequest( "config/runtimeContext.xml" ) );
When the context file is loaded, Kairos Monitor is available in your application.
You can listen all events defined in ApplicationLoaderEvent class (like classic context loading).
All url in loaded file are under IoC URL manager control.
You can take a look at this tutorial for more informations.
But we can force url to be "relative" to the context". ( sandbox rules )
In this way, all url defined in xml context ( and in included context if defined ) must be relative to the path of the loaded context file.
This technic allow to encapsulate context, plugin, file and all content in a single and dedicated folder for example.
If you want to create a clean package for Kairos Monitor, you can :
To load a runtime context using "sandbox" technic, simple define the #sandbox property to true.
var rtLoader : RuntimeContextLoader = new RuntimeContextLoader( ); rtLoader.sandbox = true;
rtLoader.addEventListener( ApplicationLoaderEvent.onApplicationInitEVENT, onComplete );
rtLoader.load( new URLRequest( "db/monitor/context.xml" ) );
Display trees can be loaded to.
<beans>
<dll url="MonitoringPluginDLL.swf" />
<root="layer"> <logo id="logo" url="../assets/logo.jpg" /> </root>
<plugin id="Monitor" type="lowra.plugins.utils.MonitoringPlugin">
<property name="x" value="200" /> <property name="y" value="0" /> <property name="alpha" value="0.5" />
</plugin>
</beans>
Important : you have to define a target container for the RuntimeContextLoader otherwise the loaded display tree is ignored.
var container : DisplayObjectContainer : BeanFactory.getInstance().locate( ContextNodeNameList.ROOT ) as DisplayObjectContainer; var rtLoader : RuntimeContextLoader = new RuntimeContextLoader( container );
rtLoader.addEventListener( ApplicationLoaderEvent.onApplicationInitEVENT, onComplete );
rtLoader.load( new URLRequest( "config/runtimeContext.xml" ) );
The id attribute of your loaded root is important as it define the container where display tree content will be inserted.
For example, you define an the id
like this :
<beans> <root="layer"> <logo id="logo" url="../assets/logo.jpg" /> </root>
</beans>
The logo is added to the container defined by the RunTimeContextLoader. ( constructor argument )
So you can get reference to this container using :
var loadedLayer : DisplayObjectContainer = BeanFactory.getInstance().locate( "layer" ) as DisplayObjectContainer;
But, if you define the id as :
<beans> <root="root"> <logo id="logo" url="../assets/logo.jpg" /> </root>
</beans>
The same id as your main context display list root object, then loaded display object will be added to this root container, and the container ( "layer" ) can no longer be accessible.
So take care of root id definition.
All loaded objects are registered in the global context, so we can use cross reference to target an object, channel or whatever you want in the global IoC context.
As RuntimeContextLoader extends ApplicationLoader class, context pre processing is also available.
You can take a look at this tutorial for more informations about pre processing.
For variables substitution, we can use URL Query definition, like this :
var rtLoader : RuntimeContextLoader = new RuntimeContextLoader( );
rtLoader.addEventListener( ApplicationLoaderEvent.onApplicationInitEVENT, onComplete );
rtLoader.load( new URLRequest( "config/runtimeContext.xml?POSITION_X=200&POSITION_Y=50" ) );