As described in 6.4 Modular Contexts Parsley allows you to build a hierarchy of modules that can be dynamically loaded and unloaded. When using Flex Modules together with Parsley it is very like that many of the Modules will require their own configuration, integrated with the configuration of the main application and loaded and unloaded along with the Module itself. With Parsley you have two options for building this Module hierarchy:
ContextModule class and not from mx.modules.Module
and then use Parsleys ModuleLoader Component for loading the Module instead of the regular Flex ModuleLoader.
This approach is described in this chapter. Writing a Parsley Context Module is only slightly different from writing a regular Flex Module. It basically involves two steps:
ContextModule base class instead of mx.modules.Module. configClass property. An example MXML Module skeleton might look like this:
<parsley:ContextModule
xmlns:mx="http://www.adobe.com/2006/mxml"
xmlns:parsley="http://www.spicefactory.org/parsley/flex"
configClass="{ShoppinCartModuleConfig}"
>
<mx:Script>
<![CDATA[
import com.bookstore.config.ShoppinCartModuleConfig;
]]>
</mx:Script>
[...]
</parsley:ContextModule>
The ShoppingCartModuleConfig we set as the configClass property is a MXML configuration
class that we would normally pass to FlexContextBuilder.build(). In this case Parsley is handling the
Context creation process for us:
ready event has fired) Parsley will create a Context
based on the configuration class specified with the configClass property. ModuleLoader
(see the next section for details). With the configClass property we cover the most common use case: each Module is associated
with a single MXML configuration file. For more complex scenarios (multiple configuration classes or using XML
configuration files) you can ignore the configClass property and overwrite the buildContext
method of the ContextModule base class instead:
<parsley:ContextModule
xmlns:mx="http://www.adobe.com/2006/mxml"
xmlns:parsley="http://www.spicefactory.org/parsley/flex"
>
<mx:Script><![CDATA[
import org.spicefactory.parsley.core.CompositeContextBuilder;
import org.spicefactory.parsley.flex.FlexContextBuilder;
import org.spicefactory.parsley.xml.XmlContextBuilder;
import flash.system.ApplicationDomain;
import com.bookstore.config.ShoppinCartModuleConfig;
public override function buildContext (parent:Context, domain:ApplicationDomain) : Context {
var builder:CompositeContextBuilder = new CompositeContextBuilder(parent, domain);
FlexContextBuilder.merge(ShoppinCartModuleConfig, builder);
XmlContextBuilder.merge("shoppingCart/logging.xml", builder);
return builder.build();
}
]]></mx:Script>
</pm:ContextModule>
Make sure that you always use the parent Context instance and the ApplicationDomain passed to the
overwritten method. In the example above we pass them to constructor of the CompositeContextBuilder.
Like with the previous example this method will be invoked by the framework as soon as the Module is
fully loaded and initialized.
You can load Parsley Context Modules with the ModuleLoader provided by the framework:
<mx:Panel
xmlns:mx="http://www.adobe.com/2006/mxml"
xmlns:parsley="http://www.spicefactory.org/parsley/flex"
addedToStage="dispatchEvent(new Event('configureIOC', true));"
>
<mx:Script>
<![CDATA[
import flash.events.Event;
import org.spicefactory.parsley.core.Context;
[Bindable]
[Inject]
public var context:Context;
]]>
</mx:Script>
<mx:TabNavigator width="100%" height="100%">
<parsley:ModuleLoader label="ShoppingCart" url="ShoppingCart.swf" parentContext="{context}"/>
<parsley:ModuleLoader label="Catalog" url="Catalog.swf" parentContext="{context}"/>
</mx:TabNavigator>
</mx:Panel>
In this example we use the view wiring support as described in 7 Flex Component Wiring. This means that
the Panel gets wired to the IOC Container. In this case we want it to inject the Context itself,
so that we don't have to explicitly pass it around. We then set the Context that this
Panel lives in to the parentContext property of the ModuleLoaders we are using.
This way we are connecting the Module Contexts to the main application Context.