Quote:
|
Originally Posted by dvanbrunt
I think your first solution is a bit over my head. If you don't mind explaining a bit more, what are the major differences in the two ways you have solved this? How with either effect other code?
Thanks
|
Well, they are kinda similar, but the important thing to remember is that pointing an object's method to a new function that you supply
(ie. xmlDoc.onLoad = function() { blah blah; }; )
is _injecting_ the function into the xmlDoc instance that you have. So, when that function gets executed, its executed in the context of the xmlDoc object. Not in any other context (ie. your object) The same thing applies with injecting a function from your class into the xmlDoc.... ie:
xmlDoc.onLoad = myXMLOnLoad;
where
ActionScript Code:
class myClass
{
function myXMLOnLoad()
{
trace("blah");
foo();
}
function foo()
{
}
}
myXMLOnLoad is a func in your class. However, as soon as you do the xmlDoc.onLoad = myXMLOnLoad; statement, when it gets executed, its as if the code was really in the XML.as file instead of the myClass.as file. Trying to call foo() won't work. Because XML.as doesn't have a foo() func.
To get around this, so that you can execute your func in your own context, you can use Delegate to get back to your context, or you can create a function object that points to your context and calls back to you.
ie. in my example, I had a
I should have highlighted it somehow. Its the important piece. This allows the function that you are creating to get back to your object and call something there.... ie:
ActionScript Code:
menuXML.onLoad = function(success)
{
if (success)
{
trace("XML Loaded");
menuthis.loadMenu(this); // <---- call back to myself
}
In this case, calling back to the myMenu object, its loadMenu method, and passing a parameter (the "this") of the XML doc that just got finished loading.
Delegate does the same thing, but Macromedia just wrapped it up in a utility class for you.
The return value of the Delegate.create() is a function that will call back into your object's context and invoke a method there.
-Trevor