Link to home
Start Free TrialLog in
Avatar of spiritwithin
spiritwithin

asked on

How to access components from inside class methods

Hi folks,

i think this is pretty straightfoward & easy ..

I am learning to use ADOBE FLEX and i am having trouble finding out about (or just understanding) the correct way to access MXML-defined components from inside a class i created, that is part of a package (which i also created). Naturally i tried using the ID of the component to refer to it. I guess its the wrong approach and it does not even work (see code).

Now, if i, for example, have a very basic TextArea in my MXML file, and i want to access this component from within any other class, what is the correct way to accomplish this?

Best regards!
// the error i get when trying to use the component through its ID
1120: Access of undefined property infoDisplay

Open in new window

Avatar of spiritwithin
spiritwithin

ASKER

After obtrusively seeking out the solution on the net, i ended up with using "Application.application" and calling the component through that.

Example:

Application.application.myTextBoxID.text = "test";

This seems weird but if there is nobody coming up with another (and actually "better") solution, i'd assume that i found the correct answer myself. I'll wait 24 hours before requesting to close this question.
It is wierd.  That way works but you are meant to use events to pass information around.

This is the initial problem that comes up when starting with Flex.  Effectively what you are doingis accessing Applicaiton as a global variable.  Have you considered looking at one of the Flex frameworks?
Avatar of zzynx
>> Application.application.myTextBoxID.text = "test";
That one works because "myTextBoxID" is defined in your main mxml. But it wouldn't work if it was in one of your other mxml files.

I agree with Jones911 that events are the way to go.

Instead of making your sub class change the main app's text area, you should let your main app listen to the sub class throwing a custom event.
When the sub class throws the custom event, your main app is triggered and can then change the content of its text area.

Have a read about Using Events in Flex: http://livedocs.adobe.com/flex/3/html/help.html?content=events_01.html
Thank you,

basically i must say that until now i did not know there was a way to import MXML files using the "import" directive.

Aside from that however, i can not agree that events triggered from components are the way to go, because for example with a loose timer i will never have an event of a specific component that triggers a specific method. Hence i can not rely on using events.
You can write your own custom events.  Trust us events are the way to pass messages between components.

Its possible and does make sense to pass messages without events in many circumstances so perhaps show some code and we can better understand your need.
>> i will never have an event of a specific component that triggers a specific method
I wouldn't know why not.
At least, I already programmed such behaviour...
My statement looses every consistence of course, given without its context. But when i am relying entirely on timers that run absolutely independetly from any components, i have no components to call events from.
>> when i am relying entirely on timers that run absolutely independetly from any components, i have no components to call events from.
Sorry, I don't understand what you're trying to say
ASKER CERTIFIED SOLUTION
Avatar of spiritwithin
spiritwithin

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial