I’ve been playing around with Flex Modules lately and thought I’d post this. It’s pretty basic, but it is kind of a “my first module” type experiment. I tried to show a few different things including: calling a module’s methods from the parent application as well as setting properties in the parent application from the loaded module.
If you haven’t looked at modules in Flex yet, I highly encourage you to check out the Flex Doc Team blog at http://blogs.adobe.com/flexdoc/, where you can find their latest version of the “Creating Modular Applications” chapter (blog entry, PDF).
Full code after the jump.
<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2007/08/06/building-a-simple-flex-module/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="vertical"
verticalAlign="middle"
backgroundColor="white">
<mx:Script>
<![CDATA[
import mx.events.VideoEvent;
[Bindable]
private var moduleTitle:String;
private var vm:VideoModule;
private function init():void {
vm = VideoModule(m1.child);
moduleTitle = vm.getModuleTitle();
}
private function stopVideo():void {
vm.stopVideo();
}
private function playPauseVideo():void {
vm.playPauseVideo();
}
]]>
</mx:Script>
<mx:Panel id="panel"
title="Module: {moduleTitle}">
<mx:ModuleLoader id="m1"
url="VideoModule.swf"
ready="init();"/>
<mx:ControlBar>
<mx:Button label="Play/Pause" click="playPauseVideo()" />
<mx:Button label="Stop" click="stopVideo()" />
<mx:Spacer width="100%" />
<mx:Label id="playheadTime" fontWeight="bold" />
</mx:ControlBar>
</mx:Panel>
</mx:Application>
<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2007/08/06/building-a-simple-flex-module/ -->
<mx:Module xmlns:mx="http://www.adobe.com/2006/mxml"
width="100%"
height="100%">
<mx:Script>
<![CDATA[
public function getModuleTitle():String {
return "Video Module";
}
/* Stop the video playback. */
public function stopVideo():void {
videoDisplay.stop();
}
/* If the video is currently playing, pause playback. Otherwise, resume playback. */
public function playPauseVideo():void {
if (videoDisplay.playing) {
videoDisplay.pause();
} else {
videoDisplay.play();
}
}
private function updateVideoTime():void {
/* If the playheadTime is 0, the DateFormatter returns an empty string.
To work around this we can default the time to 10ms if the playheadTime
is zero. */
var pTime:Date = new Date(videoDisplay.playheadTime * 1000 || 10);
var tTime:Date = new Date(videoDisplay.totalTime * 1000);
parentApplication.playheadTime.text = dateFormatter.format(pTime) + " / " + dateFormatter.format(tTime);
}
]]>
</mx:Script>
<mx:DateFormatter id="dateFormatter"
formatString="NN:SS" />
<mx:VideoDisplay id="videoDisplay"
source="http://www.helpexamples.com/flash/video/cuepoints.flv"
playheadUpdate="updateVideoTime();" />
</mx:Module>
View source is enabled in the following example.



{ 10 comments… read them below or add one }
Hi Peter,
Could you please explain and example with more detail how to call parent application from the modules. In my case i have a login module which is used by 3 different mxml applications within the same project. MXML applications are user/manager/admin interfaces which is seperated for simplicity…
Calling parent application from within the module is not easy, or i could not figure out:
in Live docs it says parentApplication calls the loader application, but it always addresses the default mxml???
I tried using Application.application.mymxml_app1 but it did not work.
In Short, how do we use a module shared by different applications, and how do we call parents from within the module?
Thanks…
PS: blog is more readable with this theme, but it needs a contact form or a dummy blog post four our questions ;)
hi,
Is it possible to load a Module from different folder other than the main application’s folder or its sub folder?
since, i’m unable to load a module in the following case,
my main app is in “root/swf/” folder
my modules are in “root/modules/” folder
my index page is in “root/htms/” folder
i’m loading the main app into the index page,
but the module are not getting loadded and it says something like , “the swf is not loadable”, what is the problem here. can you please guide me?
thanks
S. Jagan
Hi all, please help if you can:
New to flex, I’m trying to compile the SimpleModule example from the Flex 3 (and 4) SDK documentation. In both the Flex 4 and Flex3 SDK’s (command line), the module compiles fine, but the application does not. I get the error: “Access of undefined property: SimpleModule” because of this line:
sm = assetModule.factory.create() as SimpleModule;If I remove that line, the app compiles, and I can run and see that the module loads.
I’ve tried moving the module source around into different folders, keeping it at the root of the app, and nothing seems to work. Do I need to use a config file somewhere that tells the app how to use the module? do I need to include/import SimpleModule.mxml in SimpleMXMLApp.mxml? That would seem to contradict the purpose of modules. Maybe I’m missing a compiler option?
Thanks for any help.
I should clarify: Creating the simplest possible module and the simplest possible app, I can get everything to compile as long as the app doesn’t try to call a method on the module. And it runs: I can confirm that the module loads with debug output. But as soon as I add a line in the app that calls a method on the module, I get this type of error. I’m worried that the cited example assumes you already know to use a certain compiler option, or have some other file somewhere providing the information that tells the app.mxml how to access methods on the module.
How is he passing in the m1.child into the videoModule variable? I try to do this but with my own module. It is a windowedApplication as opposed to an Application for my main with the module loader, and I am running on Adobe AIR.
private var login:loginScreen;
public function init():void
{
login = new loginScreen(mainML.child);
.
.
and then here is my loginScreen (i realize that by naming convention it’s wrong I don’t care though, just trying to learn and hack something together for now).
Once again, I realize that I am using a “login” with incorrect naming convention as well as doing a log in as a module when I should just do a popup box, but I am experimenting more than anything.
for some reason this tells me that the loginScreen only allows 0 arguments in the constructor call.
Another thing that I am wanting to do is resize the main application window to match that of my login screen when the module loads but I haven’t been able to get the correct size thus far. any tips?
Dustin,
The problem is that you are passing mainML.child to the the loginScreen constuctor as opposed to casting it to a loginScreen type.
Instead of
login = new loginScreen(mainML.child);
try
login = loginScreen(mainML.child);
Flex 4.0 ,about Module to use,is not different Flex3.0,it not have atterbute “implements”,I don’t know to use it.
Hello,
Thanks for a good example.I am trying to use this module concept but i am not sure why I am getting Module value as null.Not sure how to use it exactly.
Nice tutorial, the file size appears to be pretty small too.
I have had problems with memory suck and papervision. I would guess that you memory suck problems are related to papervisions poor garbage collection and not tho module loading in flex. I saw this problem twice with papervision once it was related to how and when the rendering of 3d shapes was handled. The other time was a “ghost” type memory suck related to 3dMesh and collada files unfortunately I can’t remember the specific fix for this one.