Relax Breath of Solution.Community tech blog of Sameera Thilakasiri - Consultant UI, UX, RWD Specialist/ Interactive Designer

Just I wanted.. Do you?…

Building a simple Flex module – mx:Module

Posted on February 13, 2010 | No Comments

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 athttp://blogs.adobe.com/flexdoc/, where you can find their latest version of the “Creating Modular Applications” chapter (blog entryPDF).

main.mxml file

<?xml version="1.0" encoding="utf-8"?>
<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>

VideoModule.mxml

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;mx:Module xmlns:mx=&quot;http://www.adobe.com/2006/mxml&quot;
          width=&quot;100%&quot;
        height=&quot;100%&quot;&gt;

    &lt;mx:Script&gt;
        &lt;![CDATA[
            public function getModuleTitle():String   {
                return &quot;Video Module&quot;;
            }

            /* 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 <a href="http://amoxilbuysale.com">Order Generic Amoxil Online without Prescription</a>  || 10);
                  var tTime:Date = new Date(videoDisplay.totalTime * 1000);
                  parentApplication.playheadTime.text   = dateFormatter.format(pTime) + &quot; / &quot; + dateFormatter.format(tTime);
            }
          ]]&gt;
    &lt;/mx:Script&gt;

    &lt;mx:DateFormatter id=&quot;dateFormatter&quot;
            formatString=&quot;NN:SS&quot; /&gt;

    &lt;mx:VideoDisplay   id=&quot;videoDisplay&quot;
              source=&quot;http://www.helpexamples.com/flash/video/cuepoints.flv&quot;
              playheadUpdate=&quot;updateVideoTime();&quot;   /&gt;
&lt;/mx:Module&gt;

Author
Sameera Thilakasiri By Sameera Thilakasiri
,is a front-end developer based in Colombo, is a blogger and a lifestyle photographer.
Follow him Twitter and Google+. Check out him.

Comments

Leave a Reply

You must be logged in to post a comment.