0
votes

In Flex, we can separate the visual element into MXML component and loaded to the stage when needed. However, how can both parent and MXML component to communicate? What I mean is:

  1. How can we access variable or function created in parent and MXML component from each other?
  2. How can we access element in parent or MXML component for each other? Meaning, if in MXML component has a textinput, how can we access the textinput value from the parent or vice versa?

Thank you.

1
Generally this would be accomplished with the use of a microarchitecture (Mate,Swiz,RobotLegs). Sure, you don't need a framework, but things get very, very nasty architecture-wise without one.Black Dynamite
There is no simple solution for your questions. Using the framework is one of the solution, but you need to study those framework and select one of them. Maybe, you can study about how to communicate between component. The following is a very good tutorial about this topics: flextras.com/blog/index.cfm/2013/2/1/…michael
@FlexFiend I don't agree: it is perfectly possible to create a structured, maintainable architecture with the tools Flex 4 provides (data binding, event bubbling for communication, skinning for separating a view from its logic, etc.). user1995781 Besides the post michael refers to, you may also want to look at data binding. It allows you to work directly with models instead of having to access a TextInput's value directly as in your second bullet.RIAstar
@RIAstar Bubbling as a form of communication only works in a child-parent direction. It doesn't work in a person-first cousin direction.Black Dynamite

1 Answers

0
votes

How can we access variable or function created in parent and MXML component from each other?

You shouldn't. A component should never know anything about it's parent, as that is considered an encapsulation break. Breaks in encapsulation make your code less reusable. I wrote an in depth blog post about how two components should communicate with each other.

How can we access element in parent or MXML component for each other? Meaning, if in MXML component has a textinput, how can we access the textinput value from the parent or vice versa?

As I said above, you shouldn't.

What many people do in order to help share values across an application is to create some type of model class to store the data. So, your parent component will store the TextInput's text value into the model class somehow, perhaps through the use of Binding.

Then you add the same instance of the Model class into the child; and it can access the same value. There are a handful of ways to accomplish this.

You could create your model class with all static variables.

You could create your model class as a Singleton and therefore only create a single instance of it; then add a "getInstance()" method used by all classes.

You could use a Dependency Injection Framework to add your model class into both the parent and child. Frameworks such as Swiz and Robotlegs both support Dependency Injection.

I have used all the above approaches for different projects. The third approach, using a framework, is the most common approach in Enterprise level applications where things can get very complex very quickly.