Flex Modules communication (1)-through interface

Source: Internet
Author: User

There are many ways to communicate with Flex Modules: communication by continuing interfaces, passing parameters, and defining events. We will discuss communication through the continuation interface. Modules are independent and allow external applications to communicate with them through an inherited interface.

First, define the interface ICommunicaton.:

1:  package
2:   {
3:      public interface ICommunication
4:      {
5:          function getMessage():String;
6:   
7:          function setMessage(value:String):void;
8:      }
9:  }

 

Create a Module to inherit the ICommunicaton interface:

 1:  <?xml version="1.0" encoding="utf-8"?>
 2:  <mx:Module xmlns:mx="http://www.adobe.com/2006/mxml" implements="ICommunication" >
 3:      <mx:Script>
 4:          <![CDATA[
 5:              [Bindable]private var _value:String="";
 6:   
 7:              public function setMessage(value:String):void
 8:              {
 9:                  _value=value;
10:              }
11:   
12:              public function getMessage():String
13:              {
14:                  return _value;
15:              }
16:          ]]>
17:      </mx:Script>
18:      
19:      <mx:Panel id="panel" title="Message :{_value}" width="400" height="200"/>
20:  </mx:Module>
21:  

 

In Application, the Module method is called through ICommunication:

1:  var communication:ICommunication=moduleLoader.child as ICommunication;
2:  communication.setMessage("loaded by application");

Application code:

 

 1:  <?xml version="1.0" encoding="utf-8"?>
 2:  <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
 3:      <mx:Script>
 4:          <![CDATA[
 5:              import mx.containers.Panel;
 6:              import mx.modules.Module;
 7:   
 8:              private const MODULE_URL:String="Module1.swf";
 9:   
10:              private function onModifyMessage():void
11:              {
12:                  var communication:ICommunication=moduleLoader.child as ICommunication;
13:                  communication.setMessage("loaded by application");
14:   
15:                  var module:Module = moduleLoader.child as Module;
16:                  var panel:Panel=module.getChildByName("panel") as Panel;
17:                  trace(panel.title);
18:   
19:              }
20:          ]]>
21:      </mx:Script>
22:      
23:      <mx:HBox>
24:          <mx:Button id="btnLoad" label="Load Module" click="moduleLoader.loadModule(MODULE_URL)" />
25:          <mx:Button id="btnModify" label="Modify Module" click="onModifyMessage()"/>
26:          <mx:Button label="Unload Module" click="moduleLoader.unloadModule()" />
27:      </mx:HBox>
28:      <mx:ModuleLoader id="moduleLoader" y="30"/>
29:  </mx:Application>
30:  

 

 

 

 

Run the result. After loading the result, click Modify Module:

 

Note: although it is convenient to use the inherited interface to implement the communication between Application and Modules, the Modules memory will be locked when the Modules method is called.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.