Select to view content in your preferred language

Running an MXML widget from another MXML widget

1292
14
12-09-2010 05:12 PM
BillBass
Deactivated User
I have an Identify widget and a Search widget in a single flex application. The process is that the user performs a search for point features using the search widget, which places a graphic layer on top of points that meet the search criteria. The user can then use the Identify widget to click on those points to get information.

Instead of the user having to look for the idenify tool in the menu, I would rather have a button in the Search widget window that the user can click on to open the Identify widget. I have seen some posts related to mxmls calling other mxmls, but I think they are performing more complex operations that what I am trying to do. I am just trying to setup an alterntive method to opening an existing widget.

I am attaching a zip file with the two MXML widget files for Identify and Search. The Identify MXML also has a configuraton xml file.

Thanks.
Tags (2)
0 Kudos
14 Replies
RobertScheitlin__GISP
MVP Emeritus
Bill,

   From the calling widget use this code once you determine the widgets Id Number

Widgets are assigned a sequential number as they are loaded in the Viewer

So if you have three widgets then your widgets id would be 0, 1, or 2

SiteContainer.dispatchEvent(new AppEvent(AppEvent.WIDGET_MENU_CLICKED, false, false, 1));


I would normally suggest using a function that would return the widgets id number based off of the widgets label, but is sounds like you are wanting to keep things pretty simple.

But if you are intrested then here is the code that needs to be added to your SiteContainer.mxml
//Add this to the private declarations
private var configData:ConfigData;

//This goes in the SiteContainer Init function
//listen for the config data to be loaded
SiteContainer.addEventListener(AppEvent.CONFIG_LOADED, config);

//Add these functions any where 
private function config(event:AppEvent):void
{     
 configData = event.data as ConfigData;
}
          
public function getWidgetId(widgetLabel:String):Number
{
 var id:Number;
 for (var i:Number = 0; i < configData.configWidgets.length; i++)
 {
  if (configData.configWidgets.label == widgetLabel)
   id = configData.configWidgets.id;
 }
 return id;
}

//then you can use it like this in your code
var id:Number = SiteContainer.getInstance().getWidgetId("Identify");
SiteContainer.dispatchEvent(new AppEvent(AppEvent.WIDGET_MENU_CLICKED, false, false, id));
0 Kudos
BillBass
Deactivated User
I put the code in the Site Container.mxml as suggested. But having issues with the following:
var id:Number = SiteContainer.getInstance().getWidgetId(5);

I assume I am to put the widgetId in place of the "identify" text you specified.

Then added the following to a button:
<mx:Button label="Id Tool" click="SiteContainer.dispatchEvent(new AppEvent(AppEvent.WIDGET_MENU_CLICKED, false, false, id))"/>

I get the following error that is tied to the first line of code above:
1152: A conflict exists with inherited definition mx.core:UIComponent.id in namespace public mx.core:IDeferredInstantiationUIComponent.

What am I missing here?
0 Kudos
RobertScheitlin__GISP
MVP Emeritus
Bill,

   Actually the whole purpose of that function is to get the widgets I'd number for you from the widgets label. So enter the label of the widget as you named it in the main config.xml
0 Kudos
BillBass
Deactivated User
Still not working.

Here is what I have in my config.xml for the Identify Widget:
<widget label="Identify Features" icon="com/esri/solutions/flexviewer/assets/images/icons/i_info.png" menu="menuWidgets" config="com/esri/solutions/flexviewer/widgets/IdentifyWidget.xml" shortcutmenu="true">com/esri/solutions/flexviewer/widgets/IdentifyWidget.swf</widget>

Here is what I have in the Site Container.mxml:
private var configData:ConfigData;

This was added to the init() function in the Site Container.mxml:
SiteContainer.addEventListener(AppEvent.CONFIG_LOADED, config);

This is in the Site Container.mxml as new functions:
private function config(event:AppEvent):void
{    
configData = event.data as ConfigData;
}
  
public function getWidgetId(widgetLabel:String):Number
{
var id:Number;
for (var i:Number = 0; i < configData.configWidgets.length; i++)
{
if (configData.configWidgets.label == widgetLabel)
id = configData.configWidgets.id;
}
return id;
}

This is in the widget MXML file that will be calling the identify tool:
var id:Number = SiteContainer.getInstance().getWidgetId("Identify Features");

<mx:Button label="Id Tool" click="SiteContainer.dispatchEvent(new AppEvent(AppEvent.WIDGET_MENU_CLICKED, false, false, id))"/>
0 Kudos
BillBass
Deactivated User
Can this be done by just hard coding the widget id I want to open to a buttons click event? I don't need to do anything that automatically determines an id.
0 Kudos
RobertScheitlin__GISP
MVP Emeritus
Bill,

   Yep, you can hardcode it if you want.
0 Kudos
BillBass
Deactivated User
How would I hard code it? I believe the widget id is '5'.
0 Kudos
RobertScheitlin__GISP
MVP Emeritus
Bill,

   Look back at my first response to you and the first code block use that and just replace the number 1 with 5
0 Kudos
BillBass
Deactivated User
So where do I put that inside the button tag? Sorry, I am just confused at this point.
This is what I have for the button. I don't have any other code added to my project, just this now.
<mx:Button id="test" click="SiteContainer.dispatchEvent(new AppEvent(AppEvent.WIDGET_MENU_CLICKED, false, false, 5))"/>

When I save I get the following errors:
Access of undefined property AppEvent.
Access of undefined property SiteContainer
Call to a possibly undefined method AppEvent.
0 Kudos