06
Aug
07

Building a simple Flex module

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.

View MXML (main.mxml)

<?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>

View MXML (VideoModule.mxml)

<?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.


12 Responses to “Building a simple Flex module”


  1. 1 Okay Aug 10th, 2007 at 10:16 pm

    Easy enough, but what’s the difference/advantage of doing this vs. just creating a custom class?

  2. 2 Good Nov 7th, 2007 at 2:15 am

    I’m wondering the same thing as the previous poster, what’s the difference/advantage of doing this vs. just creating a custom class in an mxml/AS file?

  3. 3 jason Nov 29th, 2007 at 7:51 am

    I want to develop these modules as different projects. I’ve been looking for how this is done and how you would reference the different modules but I’m not having much luck. Any suggestions?

  4. 4 Greg Jan 3rd, 2008 at 7:08 pm

    I’ll take the liberty of answering the first two questions, which are important ones: why use Modules over classes or componenets?

    A module is compiled separately, as a unique SWF and is then loaded, as needed, by the shell Application. The immediate reason one would go to the trouble to work this way is that modules do not contribute to the file size of the main SWF — they are loaded progressively, much in the way we used to load additional SWFs on-the-fly in Flash with loadMovie().

  5. 5 Joe Beuckman Jan 24th, 2008 at 10:32 am

    What is the advantage of modules over just loading an application as a swf - which can be developed and tested alone, etc.

  6. 6 Joe Apr 22nd, 2008 at 8:06 pm

    你好,我来自中国,看了你写的觉得很好,但是我是属于初学者,希望能和你交个朋友,以后互相学习学习,谢谢哦!

  7. 7 prasanth May 8th, 2008 at 11:35 pm

    I want to add effects when a module loads . like a wipe left .Is it possible .Can you help me out on this

  8. 8 peterd May 9th, 2008 at 8:27 am

    prasanth,

    Does this snippet work for you?

    <mx:WipeDown id="wipeEffect" duration="6000" />
    
    <mx:Panel id="panel"
            title="Module: {moduleTitle}">
        <mx:ModuleLoader id="m1"
                url="VideoModule.swf"
                ready="init(); wipeEffect.play([m1]);" />
    

    Basically it uses the ready event to play a wipe effect.

    Peter

  9. 9 Vito May 18th, 2008 at 12:19 am

    To answer Joe’s question:
    Module vs. Application
    I turned the VideoModule into an application by just changing the module tags and then loaded it via a swf loader in the main.mxml application.

    What happens is your module and main application aren’t able to talk to each other as easily anymore. Not to mention the size of a module is about 10 times smaller than a application. You have to load all the application framework if you swf load an app but you don’t have to do that if you load a module.

    Size differences:
    [SWF] VideoApp.swf - 624,958 bytes after decompression
    [SWF] ApplicationLoader.swf - 681,000 bytes after decompression

    [SWF] VideoModule.swf - 63,301 bytes after decompression
    [SWF] main.swf - 670,323 bytes after decompression

    You can also use modules as building blocks in one application to make a modular application.

  10. 10 prasanth May 18th, 2008 at 10:39 pm

    Hey thanks a lot Peter for the snippet .It worked for me.

  11. 11 Dan D. Jun 4th, 2008 at 8:25 am

    Thank you for this very handy example. It was very useful for me while implementing Flex modules.

  12. 12 Pascal Jun 23rd, 2008 at 9:14 am

    Is there a way to load an MXML module compiled SWF into an actionscript only project?

Leave a Reply

This blog is terrible at eating HTML tags. If you plan on posting code/XML, please escape your "<" characters as "&lt;" and your ">" characters as "&gt;".