PureMVC 多模块通信 (PureMVC multi core)

项目中要用到 PureMVC 的多模块通信,就好好研究了一下这个框架。

下载:https://github.com/PureMVC/puremvc-as3-multicore-framework

网上的的教程比较多,而且也有官方的网站的demo,对我们来说真是好消息。

先来了解一些名词:

Junction 就是一个连接点,可以允许管道连接。

TeeSplit 就是一个分发消息装置。

TeeMeger就是一个接受消息装置。

Pipe 就是管道。它没有方向之分,只有跟那些消息装置连接后才会有方向。

 

那么现在开始干活吧。

第一步:在主APP初始化时候:

 

protected function application1_initializeHandler(event:FlexEvent):void
{
	junction.registerPipe(PipeAwareModule.APP_TO_MODULE_PIPE,Junction.OUTPUT,new TeeSplit());
	junction.registerPipe(PipeAwareModule.MODULE_TO_APP_PIPE,Junction.INPUT,new TeeMerge());
	junction.addPipeListener(PipeAwareModule.MODULE_TO_APP_PIPE,this,handlePipeMessage);
}

 

 这一步就是在APP上注册好分发,接受消息的装置,并侦听接受消息的装置。(参考First step 图片)

第二步:module的代码:

 

<?xml version="1.0" encoding="utf-8"?>
<mx:Module xmlns:fx="http://ns.adobe.com/mxml/2009" 
		   xmlns:s="library://ns.adobe.com/flex/spark"
		   implements="org.puremvc.as3.multicore.utilities.pipes.interfaces.IPipeAware"
		   xmlns:mx="library://ns.adobe.com/flex/mx" 
		   layout="horizontal" width="400" height="300"
		   name="module1">
	<fx:Declarations>
		<!-- Place non-visual elements (e.g., services, value objects) here -->
	</fx:Declarations>
	<fx:Script>
		<![CDATA[
			import com.model.constant.PipeAwareModule;
			
			import mx.events.FlexEvent;
			
			import org.puremvc.as3.multicore.utilities.pipes.interfaces.IPipeFitting;
			import org.puremvc.as3.multicore.utilities.pipes.interfaces.IPipeMessage;
			import org.puremvc.as3.multicore.utilities.pipes.messages.Message;
			import org.puremvc.as3.multicore.utilities.pipes.plumbing.Junction;
			import org.puremvc.as3.multicore.utilities.pipes.plumbing.Pipe;
			import org.puremvc.as3.multicore.utilities.pipes.plumbing.TeeMerge;
			import org.puremvc.as3.multicore.utilities.pipes.plumbing.TeeSplit;
			public var junction:Junction = new Junction();
			public function acceptInputPipe(name:String, pipe:IPipeFitting):void
			{
				if ( junction.registerPipe(name, Junction.INPUT, pipe)) 
				{
					junction.addPipeListener( name, this, handlePipeMessage );		
				}
			}
			
			public function acceptOutputPipe(name:String, pipe:IPipeFitting):void
			{
				junction.registerPipe( name, Junction.OUTPUT, pipe );
			}
			
			public function handlePipeMessage(message:IPipeMessage):void
			{
				switch (message.getType())
				{
					case PipeAwareModule.SHELL_MESSAGE:
					{
						text.text = message.getBody() as String;
						break;
					}
				}
			}
			
			protected function btn_clickHandler(event:MouseEvent):void
			{
				var msg:Message = new Message(PipeAwareModule.MODULE_MESSAGE,null,"Hi,I am from Module");
				junction.sendMessage(PipeAwareModule.MODULE_TO_APP_PIPE,msg);
			}
			
		]]>
	</fx:Script>
	<mx:Text id="text" text=""/>
	<s:Button id="btn" label="send" click="btn_clickHandler(event)"/>
</mx:Module>
 

在module 中你要实现org.puremvc.as3.multicore.utilities.pipes.interfaces.IPipeAware 接口,并在接受消息的管道处侦听到来的消息。

第三部:在APP中生成管道并连接APP 和 Module。

 

<mx:ModuleLoader url="ModuleApp.swf" ready="moduleloader1_readyHandler(event)"/>

 private var moduleApp:IPipeAware;

 

protected function moduleloader1_readyHandler(event:ModuleEvent):void
{
	var child:Module = event.target.child as Module;
	child.addEventListener(FlexEvent.INITIALIZE,onModuleInitialize);
}

private function onModuleInitialize(event:FlexEvent):void
{
	moduleApp = event.target as IPipeAware;
	registerJunction();
}


private function registerJunction():void
{
	var moduleToAppPipe:Pipe = new Pipe();
	moduleApp.acceptOutputPipe(PipeAwareModule.MODULE_TO_APP_PIPE,moduleToAppPipe);
	
	var appIn:TeeMerge = junction.retrievePipe(PipeAwareModule.MODULE_TO_APP_PIPE) as TeeMerge;
	appIn.connectInput(moduleToAppPipe);
	
	var appToModulePipe:Pipe = new Pipe();
	moduleApp.acceptInputPipe(PipeAwareModule.APP_TO_MODULE_PIPE,appToModulePipe);
	
	var appOut:TeeSplit = junction.retrievePipe(PipeAwareModule.APP_TO_MODULE_PIPE) as TeeSplit;
	appOut.connect(appToModulePipe);
}

 在这一步,建立管道并且分别连接APP和Module。(参见 Second Step)

第四步:APP发送消息,测试。

 

 

<s:Button label="Send to module" click="button1_clickHandler(event)"/>

 protected function button1_clickHandler(event:MouseEvent):void

{
	var msg:Message = new Message(PipeAwareModule.SHELL_MESSAGE,null,"Hi,I am Form App");
	junction.sendMessage(PipeAwareModule.APP_TO_MODULE_PIPE,msg);
}
 

至此,APP - Module和 Module - APP已经可以互相发送消息了。

 

如果有多个Module,只要在app junction 上再注册TeeSplit 和 TeeMeger(用不同的名字),然后用不同的管道连接起来app 和 module 就可以了。

然后你可以通过app 中转来实现module 和 module 之间的通信。这里就不在多说了,我会把源文件贴到附件中,可以下载下来看看。

 

 

 

你可能感兴趣的:(core)