3 Replies Latest reply on Dec 1, 2011 10:43 AM by LesBerg

    accessing a control in a parent object, programmatically using variables...

    LesBerg

      I'm working on retro-coding Role-based access into a Flex 3 web app. I need to be able to change the visibility and editability (where applicable) based on information loaded from an xml definition file.

       

      I have most of what I'm doing ironed out and working, what's holding me up is this:

      I have a list of control names as Strings, loaded from an XML file. I identify the class of the control, then change the control's 'visible', 'editable', and 'includeInLayout' with a function. The following function is in the main application container and it works correctly on controls in that container. Where the code fails is that I can't call it from children controls, which make up the bulk of the overall application.:

       

       

      public function applyControls(calledFileName:String):void
                                    { 
                                              //check
                                               for each(var control:XML in roleManager.roleInfo.role.(name == roleManager.userClaimsRole).filename.(@fileName == calledFileName).controls.control)
                                              {
                                                        var tempControlID:String = control.@controlID;
                                                        var controlClass:String = this[tempControlID].className;
                                                        if(controlClass == "HBox" || controlClass == "VBox")
                                                        {
                                                                  roleManager.controlCheckRemove(control.@visible,this[tempControlID]);
                                                        }
                                                        if(controlClass == "TextInput" || controlClass == "TextArea" || controlClass == "RichTextEditor" || controlClass == "ComboBox")
                                                        {
                                                                  this[tempControlID].visible = control.@visible;
                                                                  this[tempControlID].editable = control.@editable;
                                                        }
                                              } 
                                    }
      
      

      where calledFile is the name of the mxml file the control is located in ( I don't know if this matters)

       

      What I need to do is modify this function so I can place it in a separate .as file that handles the bulk of my role functions. Of course, calling this function from a child control fails because of the 'this' identifier.

       

      Index.mxml is the hub of the application. It calls other mxml files such as canvasLogin.mxml, canvasSearch.mxml, etc. What I need to do is modify the above function to place it in an .as file so any of the .mxml files can call the function.

       

      What syntax should replace:

       

      this[tempControlID].visible = control.@visible;
      

       

      I know I can use

      parentApplication.controlID.parameter = value
      

       

      but how do I incorporate the use of the variables into that? I tried the following, but it doesn't work. I didn't expect it to, actually, considering I was combining a call to a parent container then specifically referencing the local scope. It throws the error "Syntax error: expecting identifier before this."

       

      parentApplication.this[tempControlID].visible = control.@visible;
      

       

      similarly, this throws the error "Syntax error: expecting identifier before leftbracket."

       

      parentApplication.[tempControlID].visible = control.@visible;
      

       

      and of course, this one throws the error "Access of undefined property parentApplication."

       

      parentApplication.tempControlID.visible = control.@visible;
      

       

      I know there has to be a syntax that can do this, as there is a way to do any one or two of the things I'm trying to accomplish. I just haven't found a way to do all three at once: access a UIConponent from another file, programmaticaly, and using variables.

       

      Anyone have any suggestions?

       

      Thanks,

       

      Les

        • 1. Re: accessing a control in a parent object, programmatically using variables...
          Flex harUI Adobe Employee

          You could add another param to applyControls, use that instead of ‘this’ and have the callers pass in parentApplication or whatever is the right reference.

          • 2. Re: accessing a control in a parent object, programmatically using variables...
            LesBerg Level 1

            still working on this one....

             

            I could do this (I think):

             

            public function applyControls(callingContainer:Container, calledFileName:String):void
                                          { 
                                               for each(var control:XML in RoleInfo.role.(name == UserClaimsRole).filename.(@fileName == calledFileName).controls.contro
                                               {
                                                    var tempControlID:UIComponent = control.@controlID.toString();
                                                    var controlClass:String = tempControlID.className;
                                                    if(controlClass == "HBox" || controlClass == "VBox")
                                                    {
                                                              controlCheckRemove(control.@visible,this[tempControlID])
                                                    }
                                                    if(controlClass == "TextInput" || controlClass == "TextArea" || controlClass == "RichTextEditor" || controlClass == "ComboBox
                                                    {
                                                              Application.application.callingContainer.tempControlID = control.@visible;
            
                                                    }
                                               } 
                                          }
            
            

             

            but the problem is that the 'callingContainer' can be two or three layers farther into the application - it varies. I'm still working on it.

            e.g.;

            index.mxml --> canSecNew.mxml -->canSearch.mxml

            or

            index.mxml --> secClaims.mxml

             

            This raises another question - I'm having difficulty casting the object class as well - ComboBox doesn't like to be cast as a UIComponent, so it throws an error. I have stored the control type in the XML file along with the ID. Is there a way to create a variable and using a variable as the class:

             

            var someInterfaceControl:[type from XML file] = control.@controlID;
            

             

            Alternatively, if I could figure out how to access the control strictly using the variables, I could query the class type directly:

             

             

            Application.application.[callingContainer].[controlNameFromXMLfile].className
            

             

            Thanks for the continued help

            Les

            • 3. Re: accessing a control in a parent object, programmatically using variables...
              LesBerg Level 1

              OK - I'm being particularly dense today. I've been trying to figure out how to access the controls in the 'callingContainer' and failing at every turn.

               

              it was a real DUH moment. I'm passing 'this' as the calling container from canvasChooseKeyFor.mxml:

               

              roleManager.applyControls(this,"canvasChooseKeyFor.mxml");
              

               

              so all I have to do was use callingContainer as if it is 'this':

               

              public function applyControls(callingContainer:Container, calledFileName:String):void
                    { 
                         for each(var control:XML in RoleInfo.role.(name == UserClaimsRole).filename.(@fileName == calledFileName).controls.control)
                         {
                              var tempControlID:String =  control.@controlID.toString();
                              var tempControl:* = callingContainer[tempControlID];
              
                             if(tempControl.className == "HBox" || tempControl.className == "VBox")
                              {
                                   controlCheckRemove(control.@visible,tempControl);
                              }
                             if(tempControl.className == "TextInput" 
                             || tempControl.className == "TextArea" 
                             || tempControl.className == "RichTextEditor" 
                             || tempControl.className == "ComboBox")
                             {
                                  tempControl.visible = control.@visible == 'true'; <--note your special conversion from a previous topic (marked as 'correct answer'
                                  tempControl.editable = control.@editable == 'true';
                             }
                        } 
                   }
              
              

              Got it! Working as Intended (tm)

               

              You are the MAN!