When my code is executed in edit mode, I have a selection of API calls that need to be made, as well as one specific API call that must be made in both create and edit modes.
Here is the current code snippet:
// other controller code
var config = {};
var image = {
dc: { name: 'abc' },
cl: { name: 'def' },
ds: { name: 'ghi' },
net: { name: 'jkl' }
};
// Activate loader
vm.loading = true;
// Always retrieve list of DCs, whether in create or edit mode
promise = getDc()
.then(function(response) {
config.dc = response.data.dc;
return $q.resolve(response.data.dc);
}, function() {
return $q.reject('Failed to get DCs');
})
// In edit mode, fetch lists of Cls, Nets, and DSs if necessary.
// These are all dependent on the list of DCs.
if (editMode) {
promise
.then(function(dcs) {
image.dc = _.find(dcs, { name: image.dc.name });
return $q.all([
getCl(image.dc),
getNet(image.dc)
])
.then(function(response) {
config.cl = response[0].data.cls;
config.net = response[1].data.nets;
return $q.resolve([response[0].data.cls, response[1].data.nets]);
}, function() {
return $q.reject('Failed to get cls or nets');
});
})
.then(function(lists) {
image.cl = _.find(lists[0], { name: image.cl.name });
image.net = _.find(lists[1], { name: image.net.name });
return getDs(image.dc, image.cl)
.then(function(response) {
config.ds = response.data.ds;
return $q.resolve(response.data.ds);
}, function() {
return $q.reject('Failed to get DSs');
});
})
.then(function(dss) {
image.ds = _.find(dss, { name: image.ds.name });
return $q.resolve();
});
}
promise
.then(function() {
// Open modal here
})
.catch(function(error) {
// Print any errors
console.error(error);
})
.finally(function() {
// Deactivate loader
vm.loading = false;
});
In create mode, the modal will open directly after fetching the DCs. However, in edit mode, additional elements need to be loaded first.
The issue lies in the fact that the loader only shows until the loading of the DCs is complete, and the modal opens right after. It does not wait for other elements to finish loading.
Any suggestions?