Search code examples
actionscript-3apache-flexflex-spark

Return read-only ui component in Flex


In Flex, I want to create some variable that would hold a dictionary of ui components used throughout my application. Ideally, there would be a function in Application component that would return component for id:

public function getComponent4Id(id:String):UIComponent {}

Then I would access component using the following line:

var myComponent:UIComponent = FlexGlobals.topLevelApplication.getComponent4Id("someId");

Now, the only problem is: I want the component returned to be read-only.
It is very convenient to read some properties of myComponent from every corner of application, but I don't want my developers to abuse it and change myComponent.

Is it possible to return a copy of myComponent? Or is it possible to make it read-only somehow?


Solution

  • In the situation you describe, it is not possible to retrieve read only components.

    You can create read only variables by implementing get methods without set methods. Something like this:

    public var get myValue():UIComponent{
     return UIComponent;
    }
    

    This would allow you to retrieve the UIComponent instance, myValue, from the component; but you would not be able to set it.

    However, this would not prevent people from changing properties on the returned UIComponent unless those properties were also implemented as read only.

    I'll add that there is already a method, getChildByName() to retrieve a child component by name. If you have an instance to a parent, you can use this to access the children.

    All that said, I'm not sure I completely understand what you hope to achieve; with this functionality.