Tonight, I was faced with the following situation:
Premise:
- One component is tasked to manipulate data.
- Another component which is a custom component needs access to that data
Problem:
How do I get the data into the custom component?
The following can be found in greater detail in Adobe Flex 2 Beta 2 Adobe Document
Creating and Extending Flex Components
-> Creating Advanced MXML Components
-> Adding custom properties and methods to a component
-> Passing references to properties of MXML components
or
2436.html
Response:
There are two approaches of getting data into a custom MXML component. I describe them as “absolute” and “relative” access respectively.
Custom Component Absolute Access To A Component
In the absolute approach, you appeal to the top-level global-like Application static object reference, “mx.core.Application.application”.
function init():void
{
// Hard-coded absolute reference to application object
text = "EchoLabelAbsolute says: " + mx.core.Application.application.appLabel.text;
}
]]>
// Output
Hello World
EchoLabel says: Hello World
This is great for quick and dirty cases but bad for the long run because using hardcoded references leads to strongly coupled design. The reasons for not using this approach could be analogous to reasons for not using _root in ActionScript. Unless you can guarantee that your application will never change in the future, you will need a less-concrete approach that will be more robust to the impact of change.
Custom Component Relative Access To A Component
public var caller:Object;
function init():void
{
text = "EchoLabelRelative says: " + caller.appLabel.text;
}
]]>
// Output
I love Flex
EchoLabelRelative says: I love Flex
The benefits of performing the relative approach is that the custom component directly appeals to the calling component that contains the information rather than trying to resolve to a global root object. The latter customized component accesses the data in relative context to another component.
I have to admit, that the above is not the greatest example because both of them end up resolving to the same main application object. However, the main point in all of this is how to get information into a custom component.
Recent Comments