QuestionMy table has
md_after_save set to a script that calls
dataSource.fetchData() to refresh dependent data. When I edit several rows in a grid with
md_batch_save and press save, the reload happens once for every row. How do I make it reload only once?
AnswerCheck the
event argument: reload only when the hook is running for the last record of the batch.
Since version 1.7.28, batch save (grid with
md_batch_save, spreadsheet) calls
md_before_save and
md_after_save once per record, each time with that record (
record.<field>.value), exactly like a single save. In
md_after_save the
event variable is
{ batch: true, index, count } (record
index of
count, zero-based), and it is
null in a single save. So the guard has to cope with both cases:
if (!event?.batch || event.index === event.count - 1) {
await dataSource.fetchData();
}
A few details worth knowing:
- The hook runs after the batch has been written, and the pending changes are already marked as saved, so
fetchData() does not ask the user to confirm discarding unsaved changes. - Besides
record and event, the hook can use result (the server response for that record), isInsert, isClone, isDelete and dataSource. - In
md_before_save, returning false or throwing for one record stops the whole batch: no record is saved. - If your hooks were written before 1.7.28 and expect an array of records in one call, they must be rewritten to handle a single record.
Only the final-record guard avoids both the N reloads and the case where a reload on an early record shows half-updated data.
Documentation: Callbacks + Events