A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: Object Method CallBack Reference

  1. #1
    Senior Member
    Join Date
    Apr 2001
    Location
    UK
    Posts
    493

    Object Method CallBack Reference

    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.


    Cheers
    JOn
    Attached Files Attached Files

  2. #2
    ___________________
    Join Date
    May 2004
    Posts
    3,174
    you can use the call and apply functions to reassign scope to arbitary objects.

    functionname.call(objectreference,argument1,argmen t2);

    apply is similiar, but accepts an array in the second arguments which are concatenated during execution into individual arguments

    functionname.apply(objectreference,[argument1,argument2,argument3]);

    setInterval can work just like anything else if you dont use the suggested syntax, but rather a standard function constructor

    setInterval(function(){

    //statements.. just like you'd do with an onPress or onEnterframe;

    },1000);

  3. #3
    Monkey Moderator Lexicon's Avatar
    Join Date
    Jul 2001
    Location
    UK
    Posts
    2,038
    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?

  4. #4
    Senior Member
    Join Date
    Apr 2001
    Location
    UK
    Posts
    493
    Thanks for the replies, not used either scripts before I'll have a look and may come back with queries.

    Did you have a look at the example code?
    Just interested if either of you got it working with your solutions?

    Thanks again
    JOn

  5. #5
    Monkey Moderator Lexicon's Avatar
    Join Date
    Jul 2001
    Location
    UK
    Posts
    2,038
    Code:
    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?

  6. #6
    Senior Member
    Join Date
    Apr 2001
    Location
    UK
    Posts
    493
    You are a star mate cheers (Both of you)

    Have to delve a little deeper into the inbuilt classes. Where is this documented or just as intrestingly how did you find this?

    Had a look at the Delegate.as file and using some code (arguments.callee.target) that i havent come across before....always more to learn i guess.

    Thanks again

    Jon

  7. #7
    Monkey Moderator Lexicon's Avatar
    Join Date
    Jul 2001
    Location
    UK
    Posts
    2,038
    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?

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