PDA

View Full Version : [SOLVED] Need help on callback success after datastore reload



vaucer
8 Jan 2011, 11:39 AM
Hello all,

I try to custom a little print function founded on the forum.

I would like just to reload a datastore and add them data returned to a cell in my table, but i'm blocked at the step of reload. My loop on the datastore for populate the field is never executed or the main function continue to execute without waiting for the callback ...

My code:

I use a separate little function for reload my datastore which return true when the success callback function is true:


function getUnicRef(data){
storeUnicRef.removeAll()
storeUnicRef.reload({params: {task:"GETUNICREF",id:data},
callback: function(r, options, success) {
if(success == true){return true;} else { return false;}
}
});And my print function. Reload request are well executed i can see them in firebug but it seem that my loop is not stopped when i call getUnicRef function (sorry for my bad level):


function printGrid (grid){
var nbCol=grid.getColumnModel().getColumnCount();
var titre="<tr>";
for(i=0; i<nbCol; i++){
if(grid.getColumnModel().isHidden(i)==false)
titre=titre+"<th style='background-color:#CCCCD0;text-align:center'>"+grid.getColumnModel().getColumnHeader(i)+"</th>";
}
titre=titre+"</tr>";
var recup="";
grid.getStore().each(function(rec){
recup=recup+"<tr style='text-align:center;'>";
for(i=0; i<nbCol; i++){
if(grid.getColumnModel().isHidden(i)==false){
var dataIndex=grid.getColumnModel().getDataIndex(i);
var data=rec.get(dataIndex)
if(data){
if(dataIndex=='ID') {
var dataId = data;
} else if (getUnicRef(dataId)){
if(storeUnicRef.data.items.length!=0){
recup=recup+"<td>";
for (n = 0; n < storeUnicRef.data.items.length; n++) {
recup=recup+storeUnicRef.getAt(n).data.unicref+"</br>";
}
recup=recup+"</td>";} else { recup=recup+"<td>"+data+"</td>";}
}
} else {
recup=recup+"<td></td>";
}
}
}
recup=recup+"</tr>"
});
dataImpression="<html><head><meta http-equiv='Content-Type' content='text/html; charset=UTF-8' /><title>Impression</title><link rel='stylesheet' type='text/css' href='apac/resources/css/printstyle.css'/></head><body><table text-align='center'>"+titre+recup+"</table></div></body></html>";

var name= 'printgrid'
if(Ext.isIE6) {name='_blank';}
var win = window.open('', name);
win.document.write(dataImpression);
}example of xml doc returned when i reload my datastore with my function getUnicRef() :


Source task=GETUNICREF&id=20101120-02

<?xml version="1.0" encoding="utf-8"?>
<responses>
<total>4</total>
<response><idr>370</idr><cont><unicref>M086241AEEPKQYNAAAABKVNL</unicref></cont></response>
<response><idr>367</idr><cont><unicref>M086241AEEPKQYNAAAABKVOG</unicref></cont></response>
<response><idr>369</idr><cont><unicref>M086241AEEPKQYNAAAABKVPA</unicref></cont></response>
<response><idr>368</idr><cont><unicref>M086241AEEPKQYNAAAABKVQV</unicref></cont></response>
</responses>
Any help would be appreciated thank you very much :)

Vaucer

Condor
8 Jan 2011, 12:22 PM
callbacks are asynchonous. It's no use returning true or false from the callback, because the getUnicRef method has already returned by then.

You'll have to rewrite (and split) your entire function so it uses callbacks for all load calls.

vaucer
8 Jan 2011, 2:06 PM
Thx you Condor, i will try to another way

vaucer
9 Jan 2011, 9:48 AM
I'm still on this function ...

I try to populate an simple array with values of a datastore after reload in loop with each function on an other datastore. /:)

But now i'm facing to new problem with scope enclosure, i lost the scope on my array out of the callback method.

As you can can see in my code when a put a breakpoint on the return unicRefArr is empty...

My question is how i can just return my array out of callback function?


function printGridMulti3(grid){

var unicRefArr = [];
var unicRefVar = "";

grid.getStore().each(function(rec){
var dataIndex=grid.getColumnModel().getDataIndex(0);
var data=rec.get(dataIndex);

storeUnicRef.load({params: {task:"GETUNICREF",id:data},
callback: function(rec, options, success){
unicRefVar = "";
if(storeUnicRef.data.items.length!=0){
for (n = 0; n < storeUnicRef.data.items.length; n++) {
unicRefVar=unicRefVar+storeUnicRef.getAt(n).data.unicref+"</br>";
}
unicRefArr.push(unicRefVar);
} else {
unicRefArr.push(0);
}

}

});
});
return unicRefArr;
}By advance thank you very very very much =;

vaucer
16 Jan 2011, 1:36 PM
My workaround:



function printGrid(grid){

var unicRefArr = [];
var unicRefVar = "";
var userRefArr = [];
var userRefVar = "";
var transRefArr = [];
var transRefVar = "";
var ritmicArr = [];
var ritmicVar = "";
var p=1;



function loadUnic(data){

storeUnicRef.load({params: {task:"GETUNICREF",id:data},
callback: function(rec, options, success){

unicRefVar = "";

if(storeUnicRef.data.items.length!=0){
for (n = 0; n < storeUnicRef.data.items.length; n++) {
unicRefVar=unicRefVar+storeUnicRef.getAt(n).data.unicref+"</br>";
}
}
unicRefArr.push(unicRefVar);
sendArr(grid);
}
});
}


function loadUser(data){

storeUserRef.load({params: {task:"GETUSERREF",id:data},
callback: function(rec, options, success){
userRefVar = "";
if(storeUserRef.data.items.length!=0){
for (n = 0; n < storeUserRef.data.items.length; n++) {
userRefVar=userRefVar+storeUserRef.getAt(n).data.userref+"</br>";
}
}
userRefArr.push(userRefVar);
sendArr(grid);
}
});

}


function loadTran(data){

storeTransRef.load({params: {task:"GETRANSREF",id:data},
callback: function(rec, options, success){
transRefVar = "";
if(storeTransRef.data.items.length!=0){
for (n = 0; n < storeTransRef.data.items.length; n++) {
transRefVar=transRefVar+storeTransRef.getAt(n).data.transferref+"</br>";
}
}
transRefArr.push(transRefVar);
sendArr(grid);
}
});
}


function loadRitm(data){

storeRitmic.load({params: {task:"GETRITMIC",id:data},
callback: function(rec, options, success){
ritmicVar = "";
if(storeRitmic.data.items.length!=0){
for (n = 0; n < storeRitmic.data.items.length; n++) {
ritmicVar=ritmicVar+storeRitmic.getAt(n).data.ritmic+"</br>";
}
}
ritmicArr.push(ritmicVar);
sendArr(grid);
}
});
}



function sendArr(grid){

if(p==grid.getStore().getCount()*4){

var nbCol=grid.getColumnModel().getColumnCount();

var titre="<tr>";
for(i=0; i<nbCol; i++){
if(grid.getColumnModel().isHidden(i)==false)
titre=titre+"<th style='background-color:#CCCCD0;text-align:center'>"+grid.getColumnModel().getColumnHeader(i)+"</th>";
}
titre=titre+"</tr>";
var recup="";
var q=0;
grid.getStore().each(function(rec){
recup=recup+"<tr style='text-align:center;'>";
for(i=0; i<nbCol; i++){
if(grid.getColumnModel().isHidden(i)==false){
var dataIndex=grid.getColumnModel().getDataIndex(i);
var data=rec.get(dataIndex)


if(data){
if(dataIndex=='ID'){
var dataId = data
recup=recup+"<td>"+data+"</td>";
} else if (dataIndex=='datedeb' || dataIndex=='dateprise' || dataIndex=='daterel' || dataIndex=='datefin' || dataIndex=='datedp' || dataIndex=='datrel' || dataIndex=='debut' || dataIndex=='depuis' || dataIndex=='demarre' || dataIndex=='complete' || dataIndex=='acquitte' || dataIndex=='date' || dataIndex=='journee' || dataIndex=='juska' || dataIndex=='relance1' || dataIndex=='relance2') {
var tdata = typeof data;
if(tdata == 'object'){
data=data.dateFormat('d/m/Y H:i');
recup=recup+"<td>"+data+"</td>";} else {recup=recup+"<td>"+data+"</td>";}
} else if(dataIndex=='differ' || dataIndex=='duree' || dataIndex=='differdp' || dataIndex=='dureedp' ){
data=differender1(data);
recup=recup+"<td>"+data+"</td>";
} else if (dataIndex=="userref"){
recup=recup+"<td>"+userRefArr[q]+"</td>";
} else if (dataIndex=="unicref") {
recup=recup+"<td>"+unicRefArr[q]+"</td>";
} else if (dataIndex=="transref") {
recup=recup+"<td>"+transRefArr[q]+"</td>";
} else if(dataIndex=="ritmic") {
recup=recup+"<td>"+ritmicArr[q]+"</td>";
} else {
recup=recup+"<td>"+data+"</td>";
}
} else {
recup=recup+"<td></td>";
}
}
}
recup=recup+"</tr>"
q++;
});
dataImpression="<html><head><meta http-equiv='Content-Type' content='text/html; charset=UTF-8' /><title>Impression</title><link rel='stylesheet' type='text/css' href='apac/resources/css/printstyle.css'/></head><body><table text-align='center'>"+titre+recup+"</table></div></body></html>";

var name= 'printgrid'
if(Ext.isIE6) {name='_blank';}
var win = window.open('', name);
win.document.write(dataImpression);
}
p++;
}

grid.getStore().each(function(rec){
var dataIndex=grid.getColumnModel().getDataIndex(0);
var data=rec.get(dataIndex);

loadUnic(data)
loadUser(data)
loadTran(data)
loadRitm(data)
});

}