23 Replies Latest reply on Jun 21, 2007 12:25 PM by hciguy

    Accessing data in a class

    hciguy Level 1
      Hey guys,

      This question is hopefully simple. I have a class that loads an XML document and places it into an XML object. Does anyone know why I can't access that XML object from the XML that instantiates the class? I have a getter but it gets nothing. If I trace the XML object in the class it shows the XML but when I try to get it, it is blank....stumped.

      Thanks.
        • 1. Re: Accessing data in a class
          Rothrock Level 5
          Without seeing the structure of your class it is difficult to tell. First guesses would be.

          You are trying to access it with your code before it has loaded.

          You have a scope problem.
          • 2. Re: Accessing data in a class
            hciguy Level 1
            Here's the code. I thought it may be a scope problem but I don't know how to rectify it....
            • 3. Re: Accessing data in a class
              kglad Adobe Community Professional & MVP
              i don't see any public variables so how can you reference xml_doc? or how are you trying?
              • 4. Re: Accessing data in a class
                hciguy Level 1
                Well, originally I had declared it as public but still couldn't access it. Right now I am using the get accessor but that doesn't return anything either....either way it returns nothing...
                • 5. Re: Accessing data in a class
                  kglad Adobe Community Professional & MVP
                  trace("nodes: " + nodes) statement in your class file results in the output you expect but you instantiate a class member and fail to access its allElements property?

                  if yes, paste your code that instantiates a class member and fails to access its allElements property.
                  • 6. Re: Accessing data in a class
                    hciguy Level 1
                    Yeah I know. It traces just fine within the class. Here is the basic fla:

                    import mps.uiprototype.main.XMLparse;

                    var appMenubar:XMLparse = new XMLparse("xml/menubar.xml");

                    trace("XML Doc: " + appMenubar.allElements);

                    Then I get: "XML Doc: "

                    It's blank or empty or something. It's not null or undefined. But if I try to access that object, I do get that it is undefined

                    Thanks.
                    • 7. Re: Accessing data in a class
                      kglad Adobe Community Professional & MVP
                      you are waiting until loadComplete executes, correct? ie, i don't see an event dispatcher in loadComplete, but you are using some technique to delay the execution of that trace() function??
                      • 8. Re: Accessing data in a class
                        hciguy Level 1
                        Are you saying my access of the getter property in my FLA is trying to access the nodes object before it has loaded? That is what I thought may be happening but I don't know what to do...I figured when I instantiated a class, everything ran before I accessed that getter.

                        • 9. Re: Accessing data in a class
                          kglad Adobe Community Professional & MVP
                          the getter will return a value as soon as it's called. there's nothing in it that would make it wait for nodes to be defined.

                          and if you execute the following in your fla, it's sure to fail.

                          • 10. Re: Accessing data in a class
                            hciguy Level 1
                            Do I need to make my own event handlers to do that? I don't know how to make an event handler to handle when data is loaded or when a function is called or something...

                            Can you point in the direction where I can look? I looked at the fl.events package but it seems to apply only to components...

                            Thanks!
                            • 11. Re: Accessing data in a class
                              kglad Adobe Community Professional & MVP
                              in your loadComplete function (after nodes are defined and made public), add:

                              • 12. Re: Accessing data in a class
                                hciguy Level 1
                                Thanks kglad.

                                Now I think I am replicating code. I want the xmlLoader class to be something I can reuse whenever I need to just load in XML. So I make an instance of the class, pass it the XML file, and it returns the native object after loaded. But everytime I use it I am coyping all of the addEventListener. Maybe I have to do that every time I use it....but it seems like I should be able to contain it all in the main xmlLoader class?
                                • 13. Re: Accessing data in a class
                                  kglad Adobe Community Professional & MVP
                                  what do you mean by "...it returns the native object after loaded."? returns it where?
                                  • 14. Re: Accessing data in a class
                                    hciguy Level 1
                                    It returns the XML Object through the event to the event handler....
                                    • 15. Re: Accessing data in a class
                                      hciguy Level 1
                                      Code
                                      • 16. Re: Accessing data in a class
                                        kglad Adobe Community Professional & MVP
                                        i don't understand what you are trying to do. originally, you had an xmlloader class that loaded and parsed an xml file. in that class you assigned that xml document to a "nodes" variable.

                                        you had no trouble tracing nodes in your onComplete method, but had trouble tracing nodes outside the onComplete method. the original issue was because you were calling nodes before the onComplete executed.

                                        you tried using a getter. i assumed you wanted to pass nodes outside you class file, because there's no other point to using a getter. and you still had trouble which led me to suggest that you may be calling your getter before your onComplete had executed and therefore before nodes was defined.

                                        i therefore suggested using an event dispatcher to indicate when nodes was ready to be accessed outside your class and using a listener to detect when the onComplete had executed and nodes was defined.

                                        now, you're talking about keeping everything within your class that, i assume, loads and parses your xml data. if that's the case, you don't need a listener and you don't need an event dispatcher. you can trigger whatever actions you want directly from your onComplete method and code those actions in your class file.
                                        • 17. Re: Accessing data in a class
                                          hciguy Level 1
                                          Well, sorry for the confusion. I just wanted one class that I can use and re-use for loading XML and I want it to return the XML Object to the individual instance. So that is why I needed the event handling to get the object to the instance because the instance was trying to access it before the class had loaded it.

                                          My concern now is that every time I use the xmlLoader class, I will have to add a listener for each instance. So I use it to load the XML of the menu and I want to use it to load the XML of the application color scheme. I'm going to have all these events. So in my FLA, I instantiate colorScheme - which uses xmlLoader - and the menu object. I guess I can't get away with having to listen for these events. Maybe I need to have one super class that loads every XML I will use, and use the xmlLoader to do it, and then have one main object for the entire menu system that has one listener that is triggered once every XML document I need is loaded.

                                          Sorry. I know it is confusing. I am new to custom event handling and using classes...I'm trying my best with it. I really am.
                                          • 18. Re: Accessing data in a class
                                            kglad Adobe Community Professional & MVP
                                            there's no great harm or performance hit using several listeners. but if you want to simplify things you're in the best position to see how that can be done.
                                            • 19. Re: Accessing data in a class
                                              hciguy Level 1
                                              Well now I'm not even sure how to do it....maybe you can shed some light....

                                              Does it even make sense to make one class that simply loads XML and returns to the XML Object?

                                              Because I want to make one class that builds the menu, so I need to load two XML Documents: menubar.xml and color_scheme.xml . One has the elements of the menu and the other has the color scheme that I want to use for the entire application.

                                              But the problem now is that I can have two listeners for each XML load, but how do I know if one loads before the other? I need both to build my menu ... lol

                                              Sorry. Confusing, I know. Maybe I need to just dump it all in one XML document but that seems dumb.
                                              • 20. Re: Accessing data in a class
                                                kglad Adobe Community Professional & MVP
                                                you can use a variable to record the number of xml parsings. each time there's a successful parsing, increment that variable and check if it reveals that all needed xml files have been parsed.
                                                • 21. Re: Accessing data in a class
                                                  hciguy Level 1
                                                  Yes, I could. Good idea. So I'd have one listener that is checking to see if that variable shows they are all loaded? I'd have to use a timer, too? Something would have to check that variable, right?

                                                  What do you think about making an event "chain," so to speak. Load one XML document, which triggers the next load, until they are all loaded. But I wonder if that takes longer since they would piggy-back instead of going all at once.

                                                  Sorry for asking such simple questions. I am just trying to figure out the best way to do this stuff.
                                                  • 22. Re: Accessing data in a class
                                                    kglad Adobe Community Professional & MVP
                                                    i don't see any need for a timer. i would use a global variable so each class could update it and each class could check if all the parsings are complete. if all the parsings are complete, you could call another class to construct your menus.
                                                    • 23. Re: Accessing data in a class
                                                      hciguy Level 1
                                                      But if the global variable doesn't show it is all loaded, how will it keep checking? Use a (while) ?