board logo
WUICTeam
  • WUICTeam
  • 100% (Exalted)
  • Newbie Topic Starter
24 days ago
Update: the changeTracking input of wuic-data-source is now honoured: [changeTracking]="false" turns tracking off unless the table's extra properties set it. Fixed in WUIC Framework 1.8.2 .
The original answer below describes versions before 1.8.2.

Question
I change several rows of a wuic-data-source in memory and want to send them to the server in a single request, or throw them all away. Calling syncData once per row works, but is there a better way? And what happens if the grid reloads in the meantime?

Answer
Use the change tracker: edit the rows locally, then call batchSave() to save them all in one request, or rollbackChanges() to restore the original values. hasPendingChanges() and getPendingChanges() tell you what is waiting.

Change tracking is on by default. It is switched off only if the table's extra properties set changeTracking to false; that value is read when the metadata loads and replaces whatever you passed to the changeTracking input, so the input alone will not turn it off.


// edit in memory: every real difference is tracked per record
const rec = datasource.getClientRecordByPKey(42);
datasource.setClientRecordValue(rec, { status: 'closed' });

if (datasource.hasPendingChanges()) {
  const response = await datasource.batchSave();
  if (response === null) {
    // nothing to send, cancelled by beforeSync$, or validation failed
  }
}

// or discard everything and restore the old values
datasource.rollbackChanges();

Things to know:
  • batchSave() resolves with null when there is nothing to send, when a beforeSync$ handler cancels (the event has operation === 'batch') or when validation fails. Check for it before assuming the save happened.
  • If you set a field back to its original value, the change disappears from the tracker, so it is not sent.
  • Both methods accept a subset of changes (getPendingChanges() returns the list), so you can save or discard only some records.
  • Careful with fetchData(): with pending changes it asks the user to confirm, and if they accept, the changes are rolled back. Save before reloading, or the edits are lost.


Documentation: Datasource