views:

347

answers:

3

I have several mxml components in an app, all of which need the same variable called genericX. I've included that variable in the main mxml and made it public

[Bindable] public var genericX:Number = 102;

but I still can't access it from other mxml components. If I try to do this for example, it doesn't recognize the variable.

<s:Button x="{genericX}" label="Click" />
+1  A: 

You cannot access in this way. There is something called Events in Flex and you need to pass this variable in a MXML file to another using eventDispatcher.

For example

[Bindable] public var genericX:Number = 102;

private function init():void {

var evt:NewCustomEvent = new CustomEvent(CustomEvent.SENDDATA);
evt.genericaValue = genericX
dispatchEvent(evt);

}

Now you need to get into the MXML component where you want to recieve this Event and using addEventListner() to recieve this event and the corresponding variable.

Then finally Inject it into your button.

Vinothbabu
+1  A: 

There's also a filthy solution that works but isn't nice. You can create a static variable against the application class. For example:

[Bindable] public static var genericX : Object

You can access that from anywhere like this:

MyApplicationName.genericX

It ain't pretty, but it does work :)

simon

Simon Gladman
+1  A: 

You should be able to access any global variables with:

Flex 3:

var app:Application = mx.core.Application.application as Application;

Flex 4(looks like what you're using):

var app:Object = FlexGlobals.topLevelApplication;

And then:

<s:Button x="{app.genericX}" label="Click" />
Inigoesdr
So in the main app, instead of declaring it `public static` like `[Bindable] public var genericX:Number = 102`, what would I declare it as?
Kamo
You would declare it like you did in your original example:[Bindable] public var genericX:Number = 102It doesn't need to be static.
Inigoesdr