5 Replies Latest reply on Oct 31, 2007 5:10 PM by ntsiii

    Accessing a control from a package


      I'm sure this is pretty simple, but I'm stuck on how to perform an action like changing the visibility attribute on a progress bar from inside a package. It works when the actionscript is within the .mxml file, but not when it is inside a package that is imported.

      An example of a line that I am talking about would be:

      progress.label = "Files and folders searched: " + nodeCount;

      Where progress is the ID of a progress bar.

      Any help would be much appreciated!
        • 1. Re: Accessing a control from a package
          atta707 Level 2
          This should work!

          how do you pass the id of the progress to this function?

          Can you show more code of this function?

          Is there any other component named progress in current context?

          • 2. Re: Accessing a control from a package
            redCashion Level 1
            Hi Atta,

            I don't pass in the id of the progressbar, I just refer to it. Here is the function up to the point of the error:

            private function scanHHDir(event:FileListEvent):void
            currentNodes = event.files;
            currentSubdirectories = [];
            nodeCount += currentNodes.length;
            progress.label = "Files and folders searched: " + nodeCount;

            There isn't anything else referred to as "progress" so it is unique..

            • 3. Re: Accessing a control from a package
              ntsiii Level 3
              A "package" is a group of related classes in a single folder.

              Perhaps you mean you mean "class"?

              A class instance does not automatically have access to the scope of the parent that instantiated it. You can access the parent app scope by using Application.application. If the parent is itself a component, then just .parent. If the parent is an application loaded via SWFLoader, then use .parentDocument

              You can also pass a reference to that scope, or any other scope (that is in scope!) into the component. This requires a public property on the class, and you set that property when you instantiate the class.
              if you are instantiating the component in the main app using mxml, then do:
              <mycomps:MyComponent ... mainApp="{this}" ... />

              • 4. Re: Accessing a control from a package
                redCashion Level 1
                Thanks so much ntsiii.

                Is a line of code like Application.application.progress.visible = true; considered to be messy or undesirable in any way?
                • 5. Re: Accessing a control from a package
                  ntsiii Level 3
                  Well, it is a bit messy, but you can put that reference into a variable:
                  import mx.core.application;
                  private var _app:Application;
                  private function init():void
                  _app = Application.application;

                  then anywhere in the class:
                  _app.progress.visible = true;

                  OO purists will argue that this style of reference is bad because it creates a tight coupling between the component and the app. If yoy want to re-use the component, you will have to make sure you have a "progress" component in the main app, or you will get an error.

                  The preferred alternative is to uuse events. Have the component dispatch an event:
                  dispatchEvent(new Event("progressVisibleEvent")

                  Then in the main app, If you want to use this event, create a listener for it:
                  MyComponentId.addEventListener("progressVisibleEvent", setProgressVisible)
                  private function setProgressVisible(event:Event):void
                  this.progress.visible = true;

                  Note that this is just an example event. You would really probably set a public property on the component, say "progressVisible", to true or false. then in the event handler above, you could do:
                  var bVisible:Boolean = event.currentTarget.progressVisible;
                  this.progress.visible = bVisible;

                  While harder to write and read and debug, this approach lets you re-use your components, if that is important to you.