I'm having problems with Promises in Ext Js.
I have two promises being created through a method. One is to get an object and the other is to post an object.
The problem I'm facing is that the first time I run these promises it works fine. But after the first time the GET uses the same parameter as the first time instead of the new one. The post works fine after but with the wrong body because of the GET. Example: I run the method with "AINAPS" -> it GETs AINAPS -> posts correctly; then I run the method with "PSMS" -> it GETs "AINAPS" again -> then POSTs with "PSMS" but with "AINAPS" body.
The weird thing is everytime I close the screen and comeback it works, and then it breaks again.
It seems the first promise involving the GET is getting stuck somehow.
Do you have any idea what could be the problem here?
Thank you in advance.
Code snippets: Controller with the method:
onPauseClick: function (grid, rowIndex) {
var me = this,
gridStore = grid.getStore(),
storedTask = gridStore.getAt(rowIndex),
jobId = storedTask.get('jobId'),
activeToTime = storedTask.get('activeToTime'),
date = new Date().toISOString(),
activeToTimeIncrement = (activeToTime > date) ? 0 : 24;
gridStore.sendPauseTaskRequest(jobId, activeToTimeIncrement);
},
Store with promises and requests:
Ext.define('U4.fundamentals.backgroundjobs.store.BackgroundJobsMonitoringStore', {
autoLoad: true,
extend: 'Ext.data.Store',
alias: 'store.backgroundjobsmonitoringstore',
storeId: 'BackgroundJobsMonitoringStore',
model: 'U4.fundamentals.backgroundjobs.model.BackgroundJobsMonitoringModel',
proxy: {
type: 'rest',
api: {
read: U4.getApiPath('server-jobs'),
run: U4.getApiPath('server-jobs'),
kill: U4.getApiPath('server-jobs/{jobId}/{taskId}/kill'),
getAllConfiguration: U4.getApiPath('server-jobs-configuration/loglevel'),
getConfiguration: U4.getApiPath('server-jobs-configuration/loglevel/{jobId}'),
postJobPauseConfiguration: U4.getApiPath('server-jobs-configuration/loglevel')
}
},
sendGetConfigurationTaskRequest: function (JobId) {
var me = this,
promise = new U4.util.Promise();
me.proxy.api.getConfiguration = U4.data.proxy.UrlTemplate.replaceParameters({
jobId: JobId
}, me.proxy.api.getConfiguration);
Ext.Ajax.request({
url: me.proxy.api.getConfiguration,
method: 'GET',
success: function (data) {
promise.resolve(data);
},
failure: function (data) {
promise.reject(data);
}
});
return promise;
},
sendPauseTaskRequest: function (jobId, activeToTimeIncrement) {
var me = this;
var getStatusPromise = Promise.resolve(me.sendGetConfigurationTaskRequest(jobId));
return getStatusPromise.then(function (response) {
var sendStatusPromise = new U4.util.Promise();
var jobSettingDto = response.responseText;
var responseText = JSON.parse(jobSettingDto);
console.log(responseText);
var date = new Date();
date.setHours(date.getHours() + activeToTimeIncrement);
return Ext.Ajax.request({
url: me.proxy.api.postJobPauseConfiguration,
jsonData: JSON.stringify({
LogLevel: responseText.logLevel,
RunsRemaining: responseText.runsRemaining,
JobId: jobId,
Status: responseText.jobStatus,
ActiveToTime: date.toISOString(),
KeepTempTables: responseText.keepTempTables
}),
method: 'POST',
success: function (data) {
sendStatusPromise.resolve(data);
},
failure: function (data) {
sendStatusPromise.reject(data);
}
});
});
}
It appears that the "me.proxy.api.getConfiguration" property is being modified dynamically with"JobId" and this could lead to unexpected behavior when the same store instance is reused.
One solution is to make sure that the "me.proxy.api.getConfiguration" property is reset each time before sending a new GET request.
By defining apiPath within the "sendGetConfigurationTaskRequest" method, you ensure that the URL is constructed dynamically for each call to the function, and there won't be any unintended persistence of the previous "JobId"