I need to call back an Object method once another Object Event has Occured (DB connection return data, pass to handler and execute)
Similar to the setInterval script below:
Code:
setInterval( obj, "function", 1000 );
I want to specify the call back object and method when i call another Objects Method, this is then stored in a property and executed upon an event i.e. data returned
Am i making any sense?
I have attached a zip file of a demo selection of files that will explain it a bit further.
What is happening is on the call back the scope is of the initial caller not the recipient who needs to handle the data, hence the For loop traces the Callers Properties not the Recipients Object properties.
So my question (at last) is how does the setInterval function create a reference to the object and allow the correct scope when the method is called? As may example does not!
Any help with this would be much appreciated as it is the corner stone of a database dependant class library i am writing.
I think for a class based project, Delegate is what you are after.
Code:
import mx.utils.Delegate;
var yak:Object = new Object();
yak.myFunc = function()
{
trace("scope = "+this);
}
// var iv = setInterval(yak, 'myFunc', 1000);
var iv = setInterval(Delegate.create(this, yak.myFunc), 1000);
www.lexicon-design.co.uk
If we aren't supposed to eat animals, then why are they made of meat?
If Vegetarians like animals so much, why do they eat all their food?
class ReturnSlave
{
var correct_scope:Boolean;
function ReturnSlave()
{
correct_scope = true;
}
public function recieve (Void):Void
{
var i:String;
for(i in this)
{
trace(i+" = "+this[i])
}
}
}
swap the commented line for the Delegate line and you can see the scope switch....
Code:
class CallToReturn
{
public var returnCall:Function;
public var correct_scope:Boolean;
function CallToReturn()
{
correct_scope = false;
}
public function callFunc (obj:Object, func:String):Void
{
//returnCall = obj[func];
returnCall = mx.utils.Delegate.create(obj, obj[func]);
returnCall();
}
}
www.lexicon-design.co.uk
If we aren't supposed to eat animals, then why are they made of meat?
If Vegetarians like animals so much, why do they eat all their food?
I found it by looking at the classes, it was a Eureka moment
It's all documented in help anyways.
You should look at the EventDispatcher class too. I think you might find that useful when you need callback functions too.
www.lexicon-design.co.uk
If we aren't supposed to eat animals, then why are they made of meat?
If Vegetarians like animals so much, why do they eat all their food?