PDA

View Full Version : for loop an callback function of store load



mohaskuar
25 Jun 2013, 2:57 AM
my code
[code/]
var i;
for(i=1;i<3;i++){
console.log(i);//this display 1,2 value on firebug
if(i==1){
var s=me.getUsersLoaders1Store();
var treepanel=new Ext.create('AM.view.user.TreeLoader'+i);
}
else if(i==2){
var s=me.getUsersLoaders2Store();
var treepanel=new Ext.create('AM.view.user.TreeLoader'+i);
}else{

console.log("looperror");
}
//var f="getUsersLoaders"+1+"Store";
//var s=me.getUsersLoaders1Store();


s.proxy.extraParams.sID=sID;
s.proxy.extraParams.auto=auto;
s.load({
params: {
sID: sID,auto: auto
},
callback:function(records,option,success){
if(success){
console.log(i);//but this only shows 3 as value
if(records){
var treepanel=new Ext.create('AM.view.user.TreeLoader'+i);
var json=Ext.JSON.encode(records[0].raw);
}
}
}
});
}

[code/]
description: the value of variable "i" is different in side and out side of the callback function.....anyone know what the problem is

friend
25 Jun 2013, 5:17 AM
Keep in mind that your store.load() is asynchronous. Your for loop is probably finished executing before your first store load has returned, so i will always be 3...

mohaskuar
25 Jun 2013, 6:48 AM
so what shall i do that was the only way i thought i could solve my problem

friend
25 Jun 2013, 8:15 AM
If you need multiple Trees, then just define multiple trees and throw away the for loop.