`

Flex 调入 module 时传入参数

    博客分类:
  • Flex
阅读更多
主模块代码 
<?xml version="1.0"?>
<!-- modules/QueryStringApp.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" height="500" width="400"> 

	<mx:Script>
		<![CDATA[  
			public function initModule():void {   
				// Build query string so that it looks something like this:   
				// "QueryStringModule.swf?firstName=Nick&lastName=Danger"   
				var s:String = "QueryStringModule.swf?" + "firstName=" +   ti1.text + "&lastName=" + ti2.text;  
				
				// Changing the url property of the ModuleLoader causes   
				// the ModuleLoader to load a new module.   
				
				m1.url = s;  
			} 
		]]>

	</mx:Script>  

	<mx:Form>  
		<mx:FormItem id="fi1" label="First Name:">   
			<mx:TextInput id="ti1"/>  </mx:FormItem>  

		<mx:FormItem id="fi2" label="Last Name:">   
			<mx:TextInput id="ti2"/>  
		</mx:FormItem> 
	</mx:Form> 

	<mx:ModuleLoader id="m1"/> <mx:Button id="b1" label="Submit" click="initModule()"/>

</mx:Application>


模块部分代码

<?xml version="1.0"?>
<!-- modules/QueryStringModule.mxml -->
<mx:Module xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="parseString()"> 

	<mx:Script> 
		<![CDATA[ 
			import mx.utils.*; 

			[Bindable] private var salutation:String; 
			public var o:Object = {}; 

			public function parseString():void {  
				try {   
					// Remove everything before the question mark, including   
					// the question mark.   
					var myPattern:RegExp = /.*\?/;   
					var s:String = this.loaderInfo.url.toString();   
					s = s.replace(myPattern, "");   

					// Create an Array of name=value Strings.   
					var params:Array = s.split("&");   
					// Print the params that are in the Array.   
					var keyStr:String;   var valueStr:String;   
					var paramObj:Object = params;   

					for (keyStr in paramObj) {    
						valueStr = String(paramObj[keyStr]);   
						ta1.text += keyStr + ":" + valueStr + "\n";   
					}   

					// Set the values of the salutation.   
					for (var i:int = 0; i < params.length; i++) {    
						var tempA:Array = params[i].split("=");    
						if (tempA[0] == "firstName") {     
							o.firstName = tempA[1];    
						}    

						if (tempA[0] == "lastName") {     
							o.lastName = tempA[1];    
						}   
					}   

					if (StringUtil.trim(o.firstName) != "" &&     StringUtil.trim(o.lastName) != "") {    
						salutation = "Welcome " +    o.firstName + " " + o.lastName + "!";   
					} else {    
						salutation = "Full name not entered."   
					}  
				} catch (e:Error) {   
					trace(e);  
				}  

				// Show some of the information available through loaderInfo:  
				trace("AS version: " + this.loaderInfo.actionScriptVersion);  
				trace("App height: " + this.loaderInfo.height);  
				trace("App width: " + this.loaderInfo.width);  
				trace("App bytes: " + this.loaderInfo.bytesTotal); 

			} 

		]]> 

	</mx:Script> 

	<mx:Label text="{salutation}"/> 
	<mx:TextArea height="100" width="300" id="ta1"/>

</mx:Module>

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics