`
oncer_L
  • 浏览: 4383 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

Flex跨域加载Module

阅读更多
原文地址 : http://blog.sina.com.cn/s/blog_5ed17a7301017ql2.html

最近需要做跨平台应用加载,所以不可避免需要检索一些Flash Player安全沙箱的问题,需要注意的地方有以下几点:

1.服务器根目录存放crossdomain.xml(例Tomcat/webapps/ROOT;webservice或者其它服务器需要建空项目,发布Flex应用后使用策略域加载),这个文件的作用很明显,是用来给跨域加载模块时的权限允许文件。

2.搭配跨平台应用最好使用module来开发应用中单个功能块。这样编译后的swf就可以随处加载了(当然,properties/flex modules里面的module参数要选择none,即不为任何程序优化,这样module才可以独立运行)。

3.加载module的方法也要发生改变,远程跨域是无法直接使用moduleloader或者modulemanager直接加载的,需要使用字节流load过来后再加载,代码示例:


<?xml version="1.0" encoding="utf-8"?>
<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" minWidth="955" minHeight="600">
	<s:layout>
		<s:VerticalLayout/>
	</s:layout>
	
	<fx:Script>
		<![CDATA[
			protected function button1_clickHandler(event:MouseEvent):void
			{
				var obj:Object = {url : "ModuleTest1.swf"};
				loadRemoteModule(obj);
			}
		]]>
	</fx:Script>
	
	<fx:Declarations>
		<!-- Place non-visual elements (e.g., services, value objects) here -->
	</fx:Declarations>
	
	<fx:Script>
		<![CDATA[
			import mx.controls.Alert;
			import mx.core.IVisualElement;
			import mx.events.ModuleEvent;
			import mx.modules.IModuleInfo;
			import mx.modules.ModuleManager;
			
			private var loader:URLLoader;
			private var moduleInfo:IModuleInfo;
			private var moduleInstance:Object;
			
			private function loadRemoteModule(item:Object):void
			{
				var urlRequest:URLRequest = new URLRequest(item.url);
				loader = new URLLoader();
				loader.dataFormat = URLLoaderDataFormat.BINARY;
				loader.addEventListener(Event.COMPLETE, function urlLoader_loadCompleteHandler(event:Event):void
				{
					var styleModuleBytes:ByteArray = ByteArray(URLLoader(event.target).data);
					moduleInfo = ModuleManager.getModule("");
					moduleInfo.addEventListener(ModuleEvent.READY, onModuleReady);
					moduleInfo.addEventListener(ModuleEvent.ERROR, errorHandler);
					moduleInfo.load(null, null, styleModuleBytes);
				});
				loader.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);
				loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, errorHandler);
				loader.load(urlRequest);
			}
			private function onModuleReady(event:ModuleEvent):void
			{
				moduleInstance = event.module.factory.create();
				addElement(moduleInstance as IVisualElement);
			}
			private function errorHandler(event:Event):void
			{
				if(event is ModuleEvent)
				{
					Alert.show((event as ModuleEvent).type + ":" + (event as ModuleEvent).errorText);
				}
			}
			
		]]>
	</fx:Script>
	
	<s:Button click="button1_clickHandler(event)" label="load module test"/>
</s:Application>

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics