Javascript/Sencha
Sencha callback함수 처리
바리새인
2025. 1. 18. 23:17
앞의 함수가 끝났을 때, 작동하기 위함
callback 함수
function saveData(object, url, jsonData = null, callback = null) {
let store;
// Handle grid or store objects
if (object && typeof object.isXType === 'function' && object.isXType('grid')) {
store = object.getStore(); // Extract store from grid
} else if (object && object.isStore) {
store = object; // Directly use the store
} else {
Ext.Msg.alert('Error', 'Invalid object passed. Expected a grid or store.');
console.error('Invalid object:', object);
return;
}
const modifiedRecords = store.getModifiedRecords();
if (modifiedRecords.length > 0) {
let data = jsonData;
if (!data) {
data = modifiedRecords.map(record => record.getData());
}
Ext.Ajax.request({
url: url,
params: {
_csrf: document.getElementById('_csrf').innerText,
},
method: 'POST',
jsonData: data,
success: function (response) {
Ext.Msg.alert('Success', 'Data saved successfully.');
console.log('Server response:', response.responseText);
if (typeof callback === 'function') {
callback(); // Execute the callback if provided
}
},
failure: function (response) {
Ext.Msg.alert('Error', 'Failed to save changes.');
console.error('Server error:', response.responseText);
}
});
}
}
호출
handler: function () {
saveData(slaveStore, '/test', null, function () {
loadDataSlave(); // This will execute after saveData completes successfully
});
}