A Flash Developer Resource Site

Results 1 to 2 of 2

Thread: accessing parent class' methods (AS2) while parsing

  1. #1
    Member
    Join Date
    Sep 2003
    Posts
    39

    accessing parent class' methods (AS2) while parsing

    Hi,

    I have a class with a load() method that loads an xml file. From this method, I want to call another method from the same class, while the xml is being parsed:

    Code:
    class someClass {
    ...
    function load(url:String):Void {
    	var myxml = new XML();
    	myxml.ignoreWhite = true;
    	myxml.onLoad = function(success:Boolean) {
    		if (success) {
    			var categoryNodes:Array = this.firstChild.childNodes;	
    			for (var i = 0; i < categoryNodes.length; i++) {
    				var c:Category = addCategory(categoryNodes[i].attributes.label);
    			}
    		} else {
    			trace("There was an error parsing the XML data");
    		}
    	};
    	_xml.load(url);
    }
    
    function addCategory(label:String):Category {
    	trace("add category called");
    	var c:Category = new Category(label);
    	_categories.addItem(c);
    	return c;
    }
    }
    from my understanding, I should be able to call addCategory() like this, since it's a member method... but it doesn't work. I tried adding _parent to the path, but it doesn't work either, even if I make var myxml a class property.
    How do I access this method?

    any input would be greatly appreciated.

    best
    3rdw0rld3r

  2. #2
    Member
    Join Date
    Sep 2003
    Posts
    39
    I've found the solution at flash-coders:

    the best way would be to extend the XML class and add a property that holds a reference to the class instance, but if you're lazy you can add do this:
    Code:
    class SomeClass {
    var myxml:Object; //so you can dynamically add a property to it
    ...
    function load(url:String):Void {
    	var myxml = new XML();
    	myxml.refToClassInstance = this;
            myxml.ignoreWhite = true;
    	myxml.onLoad = function(success:Boolean) {
    		if (success) {
    			var categoryNodes:Array = this.firstChild.childNodes;	
    			for (var i = 0; i < categoryNodes.length; i++) {
    				var c:Category = this.refToClassInstance.addCategory(categoryNodes[i].attributes.label);
    			}
    		} else {
    			trace("There was an error parsing the XML data");
    		}
    	};
    	_xml.load(url);
    }
    }
    pretty neat huh... hope this is useful to somebody else too

    3rdw0rld3r

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center