PDA

View Full Version : Problem with store.load()



Naokai
18 Aug 2009, 3:09 AM
Hi im trying to calculate some values for a statistic.. now i have the problem if i call this part of the code:
i get on first call: zero.
and if i call this part a second time i get the right values..
can somebody help me out i didnt find the problem here..


Here is my Code


var positivValue = 0;
var negativValue = 0;
var turnoverValue = 0;

storeTurnover.load({
params: {
select: 'turnover',
date: date,
markt: markt
}
});

storeTurnover.data.each(function(item, index, totalItems){
if (item.get('Umsatz') > 0) {
positivValue = (parseFloat(positivValue) + parseFloat(item.get('Umsatz')));
} else {
negativValue = (parseFloat(negativValue) + parseFloat(item.get('Umsatz')));
}
});

positivValue = format_amount(Math.abs(0.85 * (positivValue)));
negativValue = format_amount(Math.abs(0.85 * (negativValue)));
turnoverValue = format_amount(positivValue - negativValue);

Lars

Condor
18 Aug 2009, 3:37 AM
load() is asynchronous. You have to wait until the server sends a response before you can start processing the data, e.g.

var positivValue = 0;
var negativValue = 0;
var turnoverValue = 0;
storeTurnover.load({
params: {
select: 'turnover',
date: date,
markt: markt
},
callback: function(){
storeTurnover.data.each(function(item, index, totalItems){
if (item.get('Umsatz') > 0) {
positivValue = (parseFloat(positivValue) + parseFloat(item.get('Umsatz')));
} else {
negativValue = (parseFloat(negativValue) + parseFloat(item.get('Umsatz')));
}
});
positivValue = format_amount(Math.abs(0.85 * (positivValue)));
negativValue = format_amount(Math.abs(0.85 * (negativValue)));
turnoverValue = format_amount(positivValue - negativValue);
}
});

Naokai
18 Aug 2009, 4:08 AM
Thank u Condor, didnt noticed that!

Lars