I have this code part, loading 3 AJAX requests and generate charts:
class IncidentStats {
constructor($statsForm) {
this.$statsForm = $statsForm;
this.charts = [];
this.$statsForm.on('submit', event => {
event.preventDefault();
this.renderCharts();
});
}
prepareFormData(key) {
const formData = this.$statsForm.serializeArray();
formData.push({
name: 'key',
value: key,
});
return formData;
}
renderCharts() {
Promise.all([
this.renderGeneralStats(),
this.renderServiceRepartitionStats(),
this.renderServerRepartitionStats(),
]).then(() => console.log('TEST'));
}
// The two other methods does nearly the same thing.
renderServiceRepartitionStats() {
return new Promise(resolve => {
$.post(this.$statsForm.attr('action'), this.prepareFormData('serviceRepartition'), data => {
this.charts['incident-service-repartition-stats'] = this.createTop10Chart(
$('#incident-service-repartition-stats'),
'Alertes Nagios',
data
);
resolve();
}, 'json');
});
}
}
I use promise in order to do some stuff at the very end of the 3 charts loading, but want to keep the loading sync.
The console.log('TEST')
on the then
callback works at the first loading.
On second form submit, the charts are indeed reloaded but the then
callback is never called.
I'm sure I'm missing something, but what? :-)
Thanks