7 Replies Latest reply on Sep 2, 2009 8:16 AM by Barna Biro

    How to get all attributes of the component in action script



      I am new to flex. I am using Action Script 3.


      I have one component with name template1.mxml. Into that I have 10 textboxes with unique id's.

      I want to clear all textboxes when the user clicks on "Clear" button.

      For this functionality, I have to wirte something like this








      I have 25 templates like this and all templates have different atttributes. The above code is becoming cumbersome for me.


      Is there any way from which I can get all attribute id's array for the object and then iterate on that array and write only one line

      Like this:-


      for each (var i in templateAttrArray ){



      I had tried the getChildren(); methods but it is returning anything.


      Can any one have solutions for this?

      Any help appreciated.



      Tushar Gandhi

        • 1. Re: How to get all attributes of the component in action script
          Gregory Lafrance Level 6

          If all the items to be cleared are TextInput, you can do this:


          for each (var obj:Object in templateObj ){

              if(obj is TextInput){

                  TextInput(obj).text = "";




          To tell you the truth, you are better off making each template a component with its own reset() method, and its reset method does something like the above.


          You can change the logic for other components as well, not just TextInput.


          If this post answers your question or helps, please mark it as such.

          1 person found this helpful
          • 2. Re: How to get all attributes of the component in action script
            Andrew Rosewarn Level 3

            Hi there, this should do it


            <?xml version="1.0" encoding="utf-8"?>
            <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" >
                        import mx.controls.TextInput;
                        private function clearAll():void {
                            for each (var ti:TextInput in vbox.getChildren()) {
                            ti.text = "";
                <mx:VBox id="vbox" x="117" y="68" height="245" width="191">
                    <mx:TextInput text="hjghj"/>
                    <mx:TextInput text="kjhkhk"/>
                    <mx:TextInput text="sdasdasd"/>
                    <mx:TextInput text="dasda"/>
                <mx:Button x="117" y="330" label="Clear" click="clearAll()"/>

            • 3. Re: How to get all attributes of the component in action script
              Barna Biro Level 3

              I partially agree with Greg. The way to go would definitely be to have a reset(); method in each component ( maybe define an interface that would be shared among components ). Although you think that your code looks cumbersom, it's not. I believe that it's still a better solution than looping trough all the components and settings all components of type TextInput to "". Why I am saying this: what if a TextInput shouldn't be reseted but it should keep the entered value? What if you implement some sort of a "hinting mechanism" that would fill the TextInput by default with some description text that would help the user understand what he is supposed to write into that field... the list could continue.


              Maybe you are not in a need of such a feature at the moment, but that doesn't mean that this will always be the case. So, having that loop that resets all objects of type TextInput in my opinion is just a temporal solution and it could prove to be quite bad on the long run ( already pointed out why... if you implement features pointed out by me and also keep the loop-reset mechanism then you'll end up adding N conditions so that the InputField that you don't want to reset, won't reset during the loop... having N condition will soon get even more awkward than what you have at the moment ).


              I'd suggest you leave your code as it is, each component should be responsible for such operations and defining N fields to reset them isn't a bad practice ( you could somehow take advantage of the looping too if you'd group your TextInput fields... you'd use the "loop-reset" mechanism only for these grouped TextInputs that you know that won't have any "special behavior/implementation" ).


              Sometimes a "generic solution" can be found and implemented, but sometimes, it's better if you stick to the "old school way" than having to rewrite a lot of code afterward if new features or new behaviors are required. Although the loop-reset approach might seem tempting for the moment and "cool", but it can just as easily lead to serious headaches and refractoring on the long run.


              Just my 2 cents,

              Barna Biro

              • 4. Re: How to get all attributes of the component in action script
                Andrew Rosewarn Level 3

                Yes I agree with Barna.  My solution was a quick fix example, and as Barna said I also came across this problem when I have a number of textFields that supplied prompting text.  My solution around it was to extend the TextInput class into my own PromptableTextInput class (as I wanted a promptable one anyway) which displayed prompt text.  The prompt text was displayed whenever the text property of the TextInput was "" or null, so in that case I was able to keep my loop that set them to "".


                With regard to the lots of templates.  Yea I also agree with the interface, that was you can call the same method on each but implement the method differently if you wish in different templates.



                • 5. Re: How to get all attributes of the component in action script
                  Barna Biro Level 3

                  Yeah, extending TextInput and implementing the new features in a way that doesn't break the looping would be a solution, but let's not forget that things can get more complicated then just simply setting an initial text ( my example was just a really breaf one ). It can easily happen that the new feature can't be implemented in way that wouldn't break the looping ( and this can really, really easily happen ).


                  In the end, it's your choice. I for one, wouldn't go for the looping solution but instead hard-code the reset method ( there's nothing wrong in hardcoding this ) or, I would sit down and anylize the situation carfully in hope of comming up with an even better solution ( since you are the one having access to your templates, only you can see "the bigger picture" clearly, so you should be the more capable person here to judge if solution X is better than solution Y - but trying to keep your stuff as open to change/flexible as possible is always a good way to go ).


                  Good luck,

                  Barna Biro


                  EDIT: In an even better scenario, instead of defining an interface, you'd define an abstract class that would be the superclass of your components ( so in essence, it will also serve as as "interface" ). The abstract class approach would be better because you can have default implmenentation of the reset(); method. You can implement the default behavior to be the loop-reset approach and only override the reset(); method in the components that "need special attention" ( where the loop-reset mechanism wouldn't work ). I believe that we could come up with a few more solution too, but IMHO, the ones we just gave you are more than enough to get you started.

                  • 6. Re: How to get all attributes of the component in action script
                    gandhi.tush Level 1


                    Yes I agreed on above all comments.

                    I will give an overview of the what I am going to do:-

                    I will be having 20-100 templates in future. I want a reset page functionality for each template.

                    For this I am going to write only one function having one parameter which will be the template object, then I am going to write one for loop which will iterate through all attributes and set all text input values to "".


                    I think this a good apporach.

                    I have one more problem.

                    I am trying the above sampe codes, like this:-

                    private function clearAll():void


                           for each (var obj:Object in  pageLayoutContainer.getChildren()) {
                                    if(obj is TextInput){
                                             TextInput(obj).text = "";


                    My mxml is like this:-

                    <mx:VBox id="pageLayoutContainer" top="20" bottom="20" left="200" right="120" width="{pageContainer.width * .8}" horizontalAlign="left" verticalAlign="top">

                    <mx:HBox id="line0">
                          <mx:Spacer width="100"/>

                          <mx:Label id="label1" text="Test" color="#993333" fontFamily="Kristen ITC" fontSize="24" fontWeight="bold"/>

                    <mx:HBox id="line1">

                      <mx:Label id="label2"text="My name is" styleName="text"/>

                          <mx:HBox id="userNameHbox">

                        <mx:TextInput id="userName" width="222" styleName="text"/>



                    If you looked at my mxml I have two HBOXes and each of them have label and textinput.

                    But above code only code only give me the first level of children(line0, line1) and not all childrens(usreName, label1, label2).

                    How should I tackle this problem?

                    Can you give me the solution?


                    Tushar Gandhi

                    • 7. Re: How to get all attributes of the component in action script
                      Barna Biro Level 3

                      That's because you are looping trough the children of the main VBox and you are forgetting that it's children might also have children and those children might also have even more children and so on. This is one of the main reasons why the loop-reset approach is bad and totally inneficient and that's why I suggested not to really use it.