10 Replies Latest reply on Sep 30, 2008 3:21 PM by Newsgroup_User

    Question about global variable scope in mxml

      Let's say I have two files, myApp.mxml and EntryClass.as, as attached at the end of this post.

      Why does mxmlc complain with this error message:
      quote:

      /path/to/EntryClass.as(4): col: 7 Error: Access of undefined property myChart.

      myChart.graphics.drawRect(100,100,100,100);
      ^



      According to devguide_flex3.pdf,
      quote:

      The IDs for all tags in an MXML component, no matter how deeply nested they are, generate public variables of the component being defined. As a result, all id properties must be unique within a document. This also means that if you specified an ID for a component instance, you can access that component from anywhere in the application: from functions, external class files, imported ActionScript files, or inline scripts.


      So how does one reference these public variables?
        • 1. Re: Question about global variable scope in mxml
          Level 1
          I believe I found the answer, which is to use parentApplication.myChart.
          • 2. Re: Question about global variable scope in mxml
            Level 1
            Actually, that doesn't work.

            "parentApplication" is not defined, and when I tried this instead:
            quote:


            var mxmlApp:Application = Application(Application.application);
            mxmlApp.myChart.graphics.drawRect(100,100,100,100);



            then mxmlc complains with this:
            quote:


            /path/to/EntryClass.as(7): col: 15 Error: Access of possibly undefined property myChart through a reference with static type mx.core:Application.

            mxmlApp.myChart.graphics.drawRect(100,100,100,100);
            ^



            Anyone know how to access myChart from EntryClass.main() as described? Thanks.
            • 3. Re: Question about global variable scope in mxml
              Peter Lorent Level 2
              Set aside that this is bad practice...
              • 4. Re: Question about global variable scope in mxml
                Level 7

                "slinabery" <webforumsuser@macromedia.com> wrote in message
                news:gbtnji$f03$1@forums.macromedia.com...
                > Let's say I have two files, myApp.mxml and EntryClass.as, as attached at
                > the
                > end of this post.
                >
                > Why does mxmlc complain with this error message:
                >
                quote:

                /path/to/EntryClass.as(4): col: 7 Error: Access of undefined property
                > myChart.
                >
                > myChart.graphics.drawRect(100,100,100,100);
                > ^
                >

                >
                > According to devguide_flex3.pdf,
                >
                quote:

                The IDs for all tags in an MXML component, no matter how deeply nested
                > they
                > are, generate public variables of the component being defined. As a
                > result, all
                > id properties must be unique within a document. This also means that if
                > you
                > specified an ID for a component instance, you can access that component
                > from
                > anywhere in the application: from functions, external class files,
                > imported
                > ActionScript files, or inline scripts.

                >
                > So how does one reference these public variables?
                >
                > myApp.mxml:
                >
                > <?xml version="1.0"?>
                > <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml"
                > applicationComplete="EntryClass.main()">
                > <mx:Panel>
                > <mx:VBox id="myChart" width="900" height="300" />
                > </mx:Panel>
                > </mx:Application>
                >
                > EntryClass.as:
                >
                > package {
                > public class EntryClass {
                > public static function main():void {
                > myChart.graphics.drawRect(100,100,100,100);
                > }
                > }
                > }

                http://www.magnoliamultimedia.com/flex_examples/Amys_Flex_FAQ.pdf
                Third question.



                • 5. Re: Question about global variable scope in mxml
                  Peter Lorent Level 2
                  >>generate public variables of the component being defined.... you can access that component from anywhere in the application

                  True, but first you need to have a reference to the object that contains that public properties...
                  • 6. Re: Question about global variable scope in mxml
                    steve_linabery
                    Thanks for the reply. Could you be more specific about which aspect of this is "bad practice?" I'm new to this framework.
                    • 7. Re: Question about global variable scope in mxml
                      Peter Lorent Level 2
                      Man, that would require a long reply :-)
                      You'll learn as you go along.
                      • 8. Re: Question about global variable scope in mxml
                        Gregory Lafrance Level 6
                        Maybe create a chart component that is somewhat self-contained, so whatever the chart needs to do, the component owns the methods, and you just create an instance of the component in the main app and call that instance's methods if necessary.
                        • 9. Re: Question about global variable scope in mxml
                          ntsiii Level 3
                          Yeah, "bad practice" does not necessarily mean bad code.

                          Briefly, in this case, using references like that make your component dependent on the parent application, which makes re-use of the component difficult if not impossible (of course, you could always copy the component, but that is not true re-use).

                          But if you component will not be re-used, then building that capablility into it migh itself be "bad practice"

                          Tracy
                          • 10. Re: Question about global variable scope in mxml
                            Level 7

                            "ntsiii" <tspratt@lariatinc.com> wrote in message
                            news:gbu45d$2ao$1@forums.macromedia.com...
                            > Yeah, "bad practice" does not necessarily mean bad code.
                            >
                            > Briefly, in this case, using references like that make your component
                            > dependent on the parent application, which makes re-use of the component
                            > difficult if not impossible (of course, you could always copy the
                            > component,
                            > but that is not true re-use).
                            >
                            > But if you component will not be re-used, then building that capablility
                            > into
                            > it migh itself be "bad practice"

                            I started out developing in an environment where it wasn't possible to keep
                            everything (or really anything) loosely coupled. And I can tell you from
                            long and hard experience that you wind up with a lot of other problems when
                            you allow parts of the application to depend on each other. For instance,
                            if I have to change anything for one of my Authorware clients, it can be a
                            HUGE PIA to figure out exactly what's going to be affected, even though the
                            entire application was designed to work together. OTOH, in Flex I am
                            obsessive about keeping everything loosely coupled, and if I need to change
                            something in Flex, typically I only change it in one place and away I go.