I'm using SmartClient_v121p_2020-04-14_PowerEdition. I have a ListGrid backed by a "clientOnly" DataSource. The grid is initialized by querying for any existing data in the database and then doing a "setCacheData" on the DataSource. Once initialized, this grid will get updated by data streaming over a websocket. See code below and the COMMENTs as to what seems to be happening in 2 different cases. After reading the documentation, I'm not clear as to which method is best to update a "clientOnly" DataSource - addData/updateData or updateCaches. Which method is considered the best practice in this case? Thank you.
Code:
function updateGrid(update) {
// If the datasource has been defined...
if( gridDS ) {
// See if the record exists already and if it does, update it,
// else, add the record
let data = gridDS.getCacheData();
let result = null;
if( data !== undefined ) {
result = data.find("id", update.id);
}
try {
// COMMENT: Case 1: This does not seem to actually update the cache as I will get
// "clientOnly add operation failed for DataSource gridDS: Duplicate key in record" errors.
if( result === null ) {
gridDS.addData(update, applyFilterCallback);
} else {
gridDS.updateData(update, applyFilterCallback);
}
// COMMENT: Case 2: This works great as the cache does seem to be getting updated,
// but I don't see a way to call a callback after the add/update happens.
// The optional parameter for the updateCaches is a DSRequest,
// which seems to be for making a call to the server, which is not desired here.
// let operationType = "add";
// if( result !== null ) {
// operationType = "update";
// }
// gridDS.updateCaches({
// data: [update],
// operationType: operationType
// });
} catch(e) {
logger.error(e);
}
}
}
Comment