OO Class that uses object with onLoad

OO Class that uses object with onLoad

Post by dbarke » Thu, 14 Oct 2004 03:43:22


I am trying to create a class that has an XML object. I want my class to be
instantiated, send a request for an XML object, then run the XML onLoad method
when it returns. The problem is that I can't figure out how the method I use
for the XML onLoad callback can call my object's methods. (In the onLoad
method, parseResults(), 'this' refers to the XML object, not my MyClass object.)

_global.MyClass = function(url) {
trace("Constructing instance of MyClass");
this.xmlObj = new XML();
this.xmlObj.ignoreWhite = true;
this.xmlObj.onLoad = this.parseResults;
this.xmlObj.load(url);
}

MyClass.prototype.parseResults = function() {
trace("onLoad callback from XML object called");

// prints the first child. See, 'this' is the XML object, not my object
trace("First node: " + this.firstChild);

// So I can't call my functions using 'this'
this.doSomething(); // this won't work
}

MyClass.prototype.doSomething = function() {
trace("doing something");
}

testMyClass = new MyClass("http://<someWebName>/<somePath>/<xmlGen.php>");

I want to be able to run other methods of MyClass, and to access other object
of MyClass.

Does anyone know how to do this? Thanks.
 
 
 

OO Class that uses object with onLoad

Post by flashphpde » Thu, 14 Oct 2004 09:02:55

It has been awhile since i have worked with actionscript 1.0 prototypes....So i
will see if i can help...

try making a holder variable for ' this ' and calling it through the xml
Method...

var myObject = this;

myObject.doSomething();

you might be able to call the method from within your ' onLoad ' method of the
xml object. this works great in actionscript 2.0 like this....

class Something extends MovieClip {

public function Something() {

}

public getData() {
var me:MovieClip = this;
var xmlObj:XML = new XML() {
xmlObj.contentType = "text/xml";
xmlObj.ignoreWhite = true;

var answerObj:XML = new XML();
answerObj.onLoad = function(success) {
if (success) {
me.doSomething();
} else {
trace("error");
}
}
}

private function doSomething() {
// Gets called from within the xml method instead of calling
the xml object itself
}

}

Hope this helps you get an idea......!
Dustin

 
 
 

OO Class that uses object with onLoad

Post by dbarke » Sat, 16 Oct 2004 06:08:46

Dustin,

I think your method would work. I found a good treatment of this issue at

Your method is handled under section 3.1 of this page. But I tried number 4
(use the Delegate method, available in Flash MX 7.2,) and it worked for me.

It turns out that the problem is with scope, and the fact that the callbacks
do not permit you to pass in the object that defines them. So you can't 'go up
a layer' to find the object that contains the object that has a callback
defined, and you can't pass a reference to that object in any of the callbacks.

Delegate tells the object with the callback to have this refer to its
container. Now to reference the callback object, you must use the private name
of that object. A little awkward, but it works.
 
 
 

OO Class that uses object with onLoad

Post by flashphpde » Wed, 20 Oct 2004 05:07:37

That is interesting I have never come across the delegate utility... Now I
know an extra bit that will help in the future..Thanks... Well I guess we now
have 2 different options to use when needed.

Dustin