PDA

View Full Version : In callback function, other functions are not accessible



syedarshadali
24 Nov 2009, 12:28 AM
//In utils.js, I have a function
this.currentUser = function( callback )
{
//PHP class method called by ExtJS Direct
var f = CMS.Direct.UserInfo.getJSON.createDelegate(null, [callback]);
f();
};


//In other js file, I am calling upper function
var callback = function(response, e)
{
if(true === e.status)
{
user = Ext.decode( response );

if( user.memberRole == 'lead' )
{

this.test();
}
}
};

utils.currentUser( callback );


//I want to call test() function from a callback function, but getting error 'this.test is not a function'
test : function()
{
alert( 'called' );
}

Any idea, why in callback function, other functions defined in the same file not accessible?

Condor
24 Nov 2009, 12:35 AM
The callback function isn't called with the correct scope.

Normally I would post the corrected code, but your example is a collection of code snippets which can't work together in their current form.

syedarshadali
24 Nov 2009, 1:01 AM
The callback function isn't called with the correct scope.

Normally I would post the corrected code, but your example is a collection of code snippets which can't work together in their current form.

Condor, how should I call it with correct scope? any sample code if you can provide.
Thanks.

Condor
24 Nov 2009, 2:48 AM
You could use:

utils.currentUser(callback.createDelegate(this));

(if 'this' is the correct scope at the time of the currentUser call - I don't know, because your code is inconsistent)

TomChiverton
27 Nov 2009, 6:51 AM
Well, I have the same problem (the result handler has no context to work with).

Code called from somewhere inside a custom ExtJS component:
Ext.coldfusion.LookupCFC.checkClient("f8923s",
function(result) {
//need to restore context of orig. call here, or at least have access to some
//variables saved before the Ext Direct call such as this.myEmbededGrid
}
);

What's the correct approach here ?

Condor
27 Nov 2009, 7:39 AM
Does checkClient support a third parameter (scope)?

In that case you could simply use:

Ext.coldfusion.LookupCFC.checkClient("f8923s", function(result){...}, this);

If it doesn't, then you should use createDelegate:

Ext.coldfusion.LookupCFC.checkClient("f8923s", (function(result){...}).createDelegate(this));

TomChiverton
27 Nov 2009, 8:01 AM
I went with the 2nd form, not wanting to dig into the guts of the Ext Direct ColdFusion connector to find out.

Using that last form appears to clobber the 'result' argument (a break point in FireBug on the first line inside the handler function shows 'result' as undefined) ?

Edit: Hold on, that was an unrelated problem (the remote method is erroring). Your suggestion works great.