PDA

View Full Version : [SOLVED] Calling a function in a certain scope (Ajax request-listener)



programmieraffe
16 Jan 2010, 2:21 AM
Hey everyone,

my question is more a general javascript question, sorry for that, but my google skills were just not enough. :">

I have a very simple handler for "requestcomplete"-events of Ext.Ajax which verifies that the xml has certain values, so that my application can handle it. I'm using a special option which I name "customSuccessFn", this function should only be called if "<response success="true">".


Ext.Ajax.on('requestcomplete', function(conn, response, options)
{

console.log('Ext.Ajax/requestcomplete event called',conn,response,options);
var dq = Ext.DomQuery; // dom query short
var xml = response.responseXML;

// functions which verifiy xml is valid for me, cutted it for forum post

if (dq.selectValue('response/@success', xml) == "true")
{
// check for customSuccessFn
if(Ext.isFunction(options.customSuccessFn))
{
options.customSuccessFn(xml, options.scope);
}
if(Ext.isDefined(options.scope) && Ext.isDefined(options.scope.options) && Ext.isFunction(options.scope.options.customSuccessFn))
{
// scope is given
if(Ext.isDefined(options.scope.options.scope))
{
options.scope.options.customSuccessFn(xml); //2DO: call this function in the given scope
}
else{
options.scope.options.customSuccessFn(xml);
}

}
}
}
,this);
This is how I call Ajax-requests (without defined scope attribute):


Ext.Ajax.request({
url:'action.php',
customSuccessFn:function(){
console.log("customSuccessFn scope",this);
// DO something
}
});Now I want to call the customSuccessFn in a certain scope, if I submit the "scope"-attribute to Ext.Ajax.request:


var grid = new Ext.grid.GridPanel({});
Ext.Ajax.request({
url:'action.php',
customSuccessFn:function(){
console.log("customSuccessFn scope",this);
// DO something
},
scope: grid // call customSuccessFn in scope of object
});Thanks in advance and general tips on how to achieve this kind of functionality are also welcome. ;)

Animal
16 Jan 2010, 2:56 AM
You should know this. It's part of the language you are using as your job!

https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Function#Methods_2

programmieraffe
16 Jan 2010, 3:26 AM
Argh, I knew it before but could not remember... Thanks Animal!