PDA

View Full Version : Loading stores synchronously



fordprefect
20 Apr 2012, 1:02 PM
Hi,

I have a store I use to grab information for how many charts I'm going to load. On the callback for that store I call another store (which gets the chart data) that gets loaded and then I want to render the chart:



// this gets the charts I need to load
this.getStore('ObjectsStore').load({
params: { id: myId },
callback: function (records, operation, success) {
for (var i = 0; i < records.length; i++) {
// for each record I get back I want to get information for that chart
var rec = records[i];

this.getStore('ChartStore').load({
params: { id: rec.id},
callback: function (records, operation, success) {
this.renderChart(records);
}
});
}
});



When I run this I get to the for loop and the chart stores start loading, ideally I'd like the first chart to finish loading and render before the second chart loads it's store and renders (basically in a synchronous fashion). As it stands now the last chart may be rendered first, etc.

Is there a way I can control the loading here?

Thanks.

skirtle
20 Apr 2012, 10:27 PM
Like this maybe?


// this gets the charts I need to load
this.getStore('ObjectsStore').load({
params: { id: myId },
callback: function (records, operation, success) {
var i = 0;

loadStore();

function loadStore() {
if (i < records.length) {
var rec = records[i];
i++;

this.getStore('ChartStore').load({
params: { id: rec.id},
callback: function (records, operation, success) {
loadStore();
this.renderChart(records);
}
});
}
}
}
});

fordprefect
23 Apr 2012, 7:14 AM
Exactly like that, thank you.