| Posted by: WUICTeam - Saturday, November 7, 2026 9:01:11 AM |
|---|
| [b]Question[/b] My table has [c]md_after_save[/c] set to a script that calls [c]dataSource.fetchData()[/c] to refresh dependent data. When I edit several rows in a grid with [c]md_batch_save[/c] and press save, the reload happens once for every row. How do I make it reload only once? [b]Answer[/b] Check the [c]event[/c] argument: reload only when the hook is running for the last record of the batch. Since version 1.7.28, batch save (grid with [c]md_batch_save[/c], spreadsheet) calls [c]md_before_save[/c] and [c]md_after_save[/c] [i]once per record[/i], each time with that record ([c]record.<field>.value[/c]), exactly like a single save. In [c]md_after_save[/c] the [c]event[/c] variable is [c]{ batch: true, index, count }[/c] (record [c]index[/c] of [c]count[/c], zero-based), and it is [c]null[/c] in a single save. So the guard has to cope with both cases: [code] if (!event?.batch || event.index === event.count - 1) { await dataSource.fetchData(); } [/code] A few details worth knowing: [list] [*]The hook runs after the batch has been written, and the pending changes are already marked as saved, so [c]fetchData()[/c] does not ask the user to confirm discarding unsaved changes. [*]Besides [c]record[/c] and [c]event[/c], the hook can use [c]result[/c] (the server response for that record), [c]isInsert[/c], [c]isClone[/c], [c]isDelete[/c] and [c]dataSource[/c]. [*]In [c]md_before_save[/c], returning [c]false[/c] 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. [/list] Only the final-record guard avoids both the N reloads and the case where a reload on an early record shows half-updated data. [url=https://wuic-framework.com/docs/callbacks-events]Documentation: Callbacks + Events[/url] |