Flexviewer两个widget间的传值

时间:2021-07-13 23:52:49

本例子是通过widgetA的按钮打开widgetB,并把widgetA的某个值传值到widgetB中,代码如下:

widgetA中的代码:

<?xml version="1.0" encoding="utf-8"?>

<viewer:BaseWidget xmlns:fx="http://ns.adobe.com/mxml/2009"
				   xmlns:s="library://ns.adobe.com/flex/spark"
				   xmlns:mx="library://ns.adobe.com/flex/mx"
				   xmlns:viewer="com.esri.viewer.*"
				   widgetConfigLoaded="init()">
	<fx:Script>
		<![CDATA[
			import com.esri.viewer.AppEvent;
			import com.esri.viewer.BaseWidget;
			import com.esri.viewer.IBaseWidget;
			import com.esri.viewer.ViewerContainer;
			
			import mx.collections.ArrayCollection;
			import mx.controls.Alert;
			import mx.events.FlexEvent;
			
			private function init():void
			{
			}
			
			var i:int=1;
			protected function button1_clickHandler(event:MouseEvent):void
			{
				var msArr:ArrayCollection = new ArrayCollection();  
				msArr.addItem("第"+i.toString()+"次传值");  
				addSharedData("test", msArr);  
				var id:Number = ViewerContainer.getInstance().widgetManager.getWidgetId("widgetB");
				var bWidget :IBaseWidget = ViewerContainer.getInstance().widgetManager.getWidget(id, true) as IBaseWidget;//打开widgetB
				i++;
			}
		]]>
	</fx:Script>
	<viewer:WidgetTemplate id="aa"
						   width="300" height="300">
		<viewer:layout>
			<s:VerticalLayout horizontalAlign="center" verticalAlign="middle"/>
		</viewer:layout>
		<s:Button label="测试" click="button1_clickHandler(event)"/>
		
	</viewer:WidgetTemplate>
</viewer:BaseWidget>

widgetB中代码:

<?xml version="1.0" encoding="utf-8"?>

<viewer:BaseWidget xmlns:fx="http://ns.adobe.com/mxml/2009"
				   xmlns:s="library://ns.adobe.com/flex/spark"
				   xmlns:mx="library://ns.adobe.com/flex/mx"
				   xmlns:viewer="com.esri.viewer.*"
				   initialize="myBaseWidgetInitHandler(event)">
	<fx:Script>
		<![CDATA[
			import com.esri.viewer.AppEvent;
			import com.esri.viewer.IBaseWidget;
			import com.esri.viewer.ViewerContainer;
			import mx.collections.ArrayCollection;
			
			import mx.controls.Alert;
			import mx.events.FlexEvent; 
			import com.esri.viewer.utils.Hashtable; 
			
			protected function myBaseWidgetInitHandler(event:FlexEvent):void
			{
				AppEvent.addListener(AppEvent.DATA_SENT, sharedDataUpdatedFrist);//widgetA第一次点击触发
				AppEvent.addListener(AppEvent.DATA_PUBLISH, sharedDataUpdatedSecond);//widgetA第二次点击后触发
				fetchSharedData();
			}

			/***widgetA第一次点击触发的事件***/
			private function sharedDataUpdatedFrist(event:AppEvent):void  
			{
				var dataTable:Hashtable = event.data as Hashtable;  
				if (dataTable.containsKey("test"))  
				{ 
					var recAC:ArrayCollection = dataTable.find("test") as ArrayCollection; 
					if (recAC[0]){
						lbl.text=recAC[0].toString();
					} 
				} 
			} 
			
			/***widgetA第二次点击后触发的事件***/
			private function sharedDataUpdatedSecond(event:AppEvent):void  
			{ 
				var data:Object = event.data; 
				if (data.key == "test") 
				{ 
					var obj:Object = data.collection[0];
					var arr:ArrayCollection = new ArrayCollection();  
					arr.addItem(obj);
					var str:String=arr.getItemAt(0).toString();
					this.lbl.text=str;
				}
			} 
		]]>
	</fx:Script>
	<viewer:WidgetTemplate id="bb"
						   width="300" height="300">
		<viewer:layout>
			<s:VerticalLayout horizontalAlign="center" verticalAlign="middle"/>
		</viewer:layout>
		<s:Label id="lbl" text="标签"/>
	</viewer:WidgetTemplate>
</viewer:BaseWidget>

运行结果:

Flexviewer两个widget间的传值