Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sharing variables between mxml components

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" />
like image 394
Kamo Avatar asked Feb 06 '26 02:02

Kamo


2 Answers

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

like image 88
Simon Gladman Avatar answered Feb 08 '26 15:02

Simon Gladman


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.

like image 40
Thalaivar Avatar answered Feb 08 '26 17:02

Thalaivar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!