Flex4 Application中与Module通信

最近忙于公司项目开发的事情,好一段时间没写Blog了,

其中有使用到Flex4在项目开发当中,于是分享一,

Application中与Module通信, 以登录为例
 

 

思路:

1. 编写接口IUserEntry:继承IEventDispatcher,并加入,设置和返回 用户名方法名;

2.新建Module,实现IUserEntry 接口,并新建 登录事件,并颁发登录事件绑定;

3.在Application中,创建并使用接口和实例;

 

主要代码:

//IUserEntry.as 接口
package com.oreilly.f4cb
{
    import flash.events.IEventDispatcher;
    public interface IUserEntry extends IEventDispatcher
    {
        function getFullName():String;
        function get firstName():String;
        function set firstName( str:String ):void;
        function get lastName():String;
        function set lastName( str:String ):void;
    }
}

 

//myModule.mxml

<mx:Module implements="com.oreilly.f4cb.IUserEntry"
           xmlns:fx
="http://ns.adobe.com/mxml/2009"
           xmlns:s
="library://ns.adobe.com/flex/spark"
           xmlns:mx
="library://ns.adobe.com/flex/mx"
           layout
="vertical" width="100%" height="100%">
    
<fx:Metadata>
        [Event(name="submit", type="flash.events.Event")]
    
</fx:Metadata>
     
<fx:Script>
        
<![CDATA[
            private var _firstName:String;
            private var _lastName:String;
            private function handleSubmit():void
            {
                firstName = firstNameInput.text;
                lastName = lastNameInput.text;
                dispatchEvent( new Event( "submit" ) );
            }
            public function getFullName():String
            {
                return _lastName + ", " + _firstName;
            }
            [Bindable]
            public function get firstName():String
            {
                return _firstName;
            }
            public function set firstName( value:String ):void
            {
                _firstName = value;
            }
            [Bindable]
            public function get lastName():String
            {
                return _lastName;
            }
            public function set lastName( value:String ):void
            {
                _lastName = value;
            }
        
]]>
   
</fx:Script>
    
<s:HGroup verticalAlign="bottom">
        
<s:Label text="First Name:" />
        
<s:TextInput id="firstNameInput" width="100%" />
    
</s:HGroup>
    
<s:HGroup verticalAlign="bottom">
        
<s:Label text="Last Name:" />
        
<s:TextInput id="lastNameInput" width="100%" />
    
</s:HGroup>
    
<s:Button label="submit" click="handleSubmit();" />
</mx:Module>

 

//ComunicationWithModule.mxml

<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
               xmlns:s
="library://ns.adobe.com/flex/spark"
               xmlns:mx
="library://ns.adobe.com/flex/mx">
    
<fx:Script>
        
<![CDATA[
            import com.oreilly.f4cb.IUserEntry;
            private var myModule:IUserEntry;
            private function handleModuleReady():void
            {
                myModule = moduleLoader.child as IUserEntry;
                myModule.addEventListener( "submit", handleSubmit );
            }
            private function handleSubmit( evt:Event ):void
            {
                welcomeField.text = "Hello " + myModule.getFullName();
            }
            public function getInformation():String
            {
                return "Greetings!";
            }
        
]]>
    
</fx:Script>

 

posted @ 2011-07-04 08:37  Marksion  阅读(1006)  评论(0编辑  收藏  举报