6 Replies Latest reply on Jun 16, 2007 5:25 PM by mouseit

    Scoping issue

    mouseit
      Okay, a bit of a mouthful here but (I think) my problem is about scoping. I have a class and part of it is an XML object (as a property). The onload handler of the XML object has to be able to modify the data in the parent structure. I have no idea whatsoever how to do this - I may have to resort to some kind of arcane message passing system using the _root scope. Any help is appreciated.
      -Mouseit
        • 1. Re: Scoping issue
          SymTsb Level 2
          You may want to provide a segment of code to help us better understand. Scope is more easily addressed when we can see the actual code
          • 2. Scoping issue
            Greg Dove Level 4
            I've made an assumption about what you're asking... although like SymTsb says its difficult to be sure without a code snippet.

            If I'm assuming correctly what your issue is then one way I do it is by assigning a .parent (or owner or whatever) property with a reference to this to the XML object after I create it. I don't know if its good programming practice - probably not...but it works for me.

            var myXML:XML = new XML()

            myXML.parent = this;

            ignoreWhite etcetera...

            myXML.onLoad = function() {
            // this.parent now refers to the timeline in here or the class instance if this code is inside a class

            }

            • 3. Re: Scoping issue
              mouseit Level 1
              Thanks, (below) is some example code and as for the parent idea, it sadly doesn't work:
              **Error** D:\Flash\FeedProcessor.as: Line 20: There is no property with the name 'parent'.
              this.xmlLoader.parent = this

              Total ActionScript Errors: 1 Reported Errors: 1
              No idea why the property is read-only.

              Sample Code:
              class rssHandler{
              var xmlLoader:XML;
              var resultXML:String;
              function rssHandler(){
              xmlLoader.onLoad = function(){
              //somehow set the resultXML property of the owner to the
              value of (for example) xmlLoader.firstChild.nodeValue
              }
              }
              }
              • 4. Re: Scoping issue
                SymTsb Level 2
                that's because onLoad doesn't have a parent property. do this instead....

                xmlLoader.onLoad = function(){
                var xmlObj:rssHandler = this;
                xmlObj._parent.whatever_you_need_to_target
                }
                • 5. Scoping issue
                  Greg Dove Level 4
                  Sorry. I see what you mean.

                  I don't know if this will work - haven't tried it. But it will not trigger an error:

                  xmlLoader['parent'] = this;


                  ... and inside the onLoad:

                  xmlLoader.onLoad = function(){

                  this['parent'].resultXML = this.toString()
                  }
                  • 6. Re: Scoping issue
                    mouseit Level 1
                    Woohoo! The latter worked handily (I didn't totally understand the former =) - thank you both very much!