View Full Version : Passing variables between MXML files / components - Flex component communication

10-25-2006, 09:05 AM

I am having difficulty with my Flex Application… I don’t know if I am just being stupid and missing / mi-understanding something (which is probably the case) but thing don’t seem to be going my way :(

My application is quite large now, so I will try and explain / include much summarised detail and code examples.

Basically, I have my main MXML File which includes an applicationControlBar containing a linkBar populated using a viewStack as its dataProvider….
The application navigator “links” to other components (MXML files) within the application that are stored in a “components” folder:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns:mi="components.*" layout="absolute">

<mx:ApplicationControlBar id="appBar_apc" dock="true">
<mx:LinkBar dataProvider="MenuObjects_lb"/>

<mx:ViewStack id="MenuObjects_lb">

<mi:homePage id="home_page" label="Homepage"/>
<mi:search id="Search" label="Search"/>
<mi:manageUsers id="manage_usrs" label="Manage Users"/>
<mi:viewOPG id="view_OPG" visible="false"/>


The 1st page the user sees is the “Homepage” this page contains a dataGrid that is provided data from a webService – calling the webService and setting up a dataProvider accordingly to populat the dataGrid etc….
The data provided from the dataProvider comes originally from a database and each row contains a unique ID.
The data is displayed in the dataGrid ok, and the ID is assigned ok, and when a row of the dataGrid is selected the correct ID of the selected row is taken – so all is ok to this point:

<?xml version="1.0" encoding="utf-8"?>
<mx:VBox xmlns:mx="http://www.adobe.com/2006/mxml" xmlns:mi="components.*">

public function handleWSR(event:ResultEvent):void
myDG.dataProvider = event.result;

public function dgItemSelect():void
selectedID = myDG.selectedItem.id;

<mx:WebService id="wsData" wsdl="http://...?wsdl">
<mx:operation name="getData" result="handleWSR(event)"/>

<mx:Panel id="home_page_test" title="Homepage Test">

<mx:DataGrid id="myDG" change="dgItemSelect()">
<mx:DataGridColumn id="id" dataField="id" headerText="ID"/>
<mx:DataGridColumn dataField="a" headerText="A"/>
<mx:DataGridColumn dataField="b" headerText="B"/>
<mx:DataGridColumn dataField="c" headerText="C"/>
<mx:DataGridColumn dataField="D" headerText="D"/>

<mx:Button id="viewItem" label="View Selected Item" click="????"/>


as you can see – beneath my dataGrid is a button (View) which when clicked I want to take the ID of the selected dataGrid row and pass this ID to another MXML file / component (viewRow.mxml) within my application.
When the “viewRow.mxml” is opened / called I want it to take the ID that was selected from the previous mxml component (the homepage) and use it in another webService call (send the ID to a webService which then uses the ID to retrieve data that is relevant to that ID and thus pass it back to the component) using and displaying the results accordingly…..

Mmmm, I hope that makes sense?

Like I say, I am having great difficulty passing variable from one MXML file to another allowing such dynamic operations – passing and retrieving variables between components (displaying data relevant to a selected ID from another components etc…)
I don’t know if I am doing something wrong (I am quite new to Flex and this is my first “real” Flex application), hence I am asking for some advice, so any suggestions / advice on this would be much appreciated.


10-25-2006, 11:48 AM
I'm kind of a newbie myself but this might help you. If you use a strategy similar to what is explained here in the Flex help under "Passing Data to and from a popup window" Here's a link to it on the web but it's also in your Help contents. http://livedocs.macromedia.com/flex/2/docs/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs_Parts&file=00000701.html

Another thought is that if your component is a class then you can just put the ID as part of the constructor.

If I'm missing something let me know.

Hope this helps,

10-25-2006, 03:26 PM
Hi Christopher...

I read throught the example in the link you posted - interesting!

it is the sort of thing I am after... but I need / want another MXML component to display / show NOT as a pop up window, but just generally in the application, overriding the current MXML file... (if that makes sense)

where they use the popUp stuff (PopUpManager etc..) I want to just "show" the selected MXML component rather than "pop it up" etc....

do you know of some sort of similar method I can use to just "show" my selected MXML file????

Thanks a lot,

10-25-2006, 08:40 PM
Yes but really all the popup window is IS another component. So the same techniques can be applied.

10-27-2006, 04:54 PM
I'm also unsure as to what exactly you want to do, but it sounds to me like you want to use States (not an actual popup browser window?).

You can use States to define ... well, States... of your application. So say you want a login screen to appear (your first State), but then once you've logged in, you want to display your real user interface (a second State).