• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 551
  • Last Modified:

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

0
spiritwithin
Asked:
spiritwithin
  • 4
  • 3
  • 2
1 Solution
 
spiritwithinAuthor Commented:
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.
0
 
Jones911Commented:
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?
0
 
zzynxSoftware engineerCommented:
>> 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
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
spiritwithinAuthor Commented:
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.
0
 
Jones911Commented:
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.
0
 
zzynxSoftware engineerCommented:
>> 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...
0
 
spiritwithinAuthor Commented:
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.
0
 
zzynxSoftware engineerCommented:
>> 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
0
 
spiritwithinAuthor Commented:
I mean that if i am using a Timer that frequently calls a method of a class which then wants to deal with one of the components in my application, i have no event of any of those components, which i could use to trigger a / this method. Thus i end up with a method that is triggered by a timer but both have no practical connection to any component.

Anyway, given the fact that i can "import" MXML declarations/definitions into any package, the ID of the components contained becomes available in that scope. This is what i was looking for, and it is a better way to deal with the issue, instead of using Application.application to refer to the component.

I am thankful for tries to help me, and appreciate the people's efforts.
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now