PDA

View Full Version : Scope problem once again and yes I read



Elijah
27 Nov 2009, 11:24 AM
Yes I read the small tutorial on scope but that did not help me.

I have 2 issues both related to scoping.

Using the desktop example from ExtJS I am populating a grid with data from an ajax call. This functions just fine. The problem is that I want to set it up to call the service lets say every 30 seconds. I thought I could use setInterval(fn, delay) to call the function that makes the ajax call but it can not find the method.

Can someone post a snippet on how to call the my custom function ajaxCall .


MyDesktop.AlertWindow = Ext.extend(Ext.app.Module, {
id:'alert-win',
init : function(){
// Defines the start menu item
this.launcher = {
text: 'Alerts',
iconCls:'icon-grid',
handler: this.getAlerts,
scope: this
}
},

getAlerts: function(){
// problem here is that when setInterval trips, it this.ajaxCall() is not a function
alertIntervalId = setInterval('this.ajaxCall()', 3*1000);
},

ajaxCall : function(){
// notice the self set to this for scope
self = this;

Ext.Ajax.request({
url : 'modules/alerts/alerts.cfm',
params : {action:'getdate'},
method : 'GET',
success: function (result, request) {
Ext.MessageBox.alert('Success', 'Data returns from the server:\n' + result.responseText);
dataSource = result.responseText;
try{
dataSource = result.responseText;
dataSource = Ext.util.JSON.decode(dataSource);
// Call method to display the data
// Is there a better way than creating a self var set to this for scope issue?
self.displayAlerts(dataSource);
}
catch(e)
{
alert('ajaxCall Exception: \n' + e.message);
// clear the interval to prevent constant calls
clearInterval(ajaxIntervalAlertId);
return;
} // try
},
failure: function (result, request) {
Ext.MessageBox.alert('Failed', 'No data from the server:\n' + result.responseText);
return;
}
});
}, // ajaxCall

VinylFox
27 Nov 2009, 12:51 PM
Use Task Manager to take care of that, it can handle setting scope, get rid of setInterval.

Ext.TaskMgr (http://www.extjs.com/deploy/dev/docs/?class=Ext.TaskMgr)

Example:

var task = {
run: this.ajaxCall,
interval: 3*1000,
scope: this
}
Ext.TaskMgr.start(task);

Elijah
29 Nov 2009, 3:05 PM
Vinyl, you bring up another question for me. Based on the syntax of the code I have where and what syntax would I use in order to define a variable such as that? Else I am to put it after the whole "MyDesktop.AlertWindow = Ext.extend(Ext.app.Module, {
..." block of code?

VinylFox
29 Nov 2009, 7:07 PM
It can be used in place of setInterval.

Elijah
30 Nov 2009, 5:24 AM
Yeah, the setinterval appears to be a bad idea. after a few minutes FF locks up. Using your code does not cause the same issue.

var task = {
run: this.ajaxCall,
interval: 3*1000,
scope: this
}
Ext.TaskMgr.start(task);

Thanks VinylFox

dearsina
5 Mar 2010, 3:22 AM
VinylFox, thanks! This was just was I was looking for!