src/FormStore.js
async function processSaveResponse(store, updates, response) {
  store.options.log(`[${store.options.name}] Response received from server.`);

  if (response.status === 'error') {
    action(() => {
      let errorFields = [];
      if (response.error) {
        if (typeof response.error === 'string') {
          store.serverError = response.error;
        } else {
          Object.assign(store.dataErrors, response.error);
          errorFields = Object.keys(response.error);
        }
      }

      // Supports an array of field names in error_field or a string
      errorFields = errorFields.concat(response.error_field);
      errorFields.forEach((field) => {
        if (store.options.autoSaveInterval && !store.dataErrors[field] && store.isSame(updates[field], store.data[field])) {
          store.data[field] = store.dataServer[field]; // revert or it'll keep trying to autosave it
        }
        delete updates[field]; // don't save it as the new dataServer value
      });
    })();
  } else {
    store.serverError = null;
  }

  Object.assign(store.dataServer, updates);

  action(() => {
    if (response.data) {
      Object.assign(store.dataServer, response.data);
      Object.assign(store.data, response.data);
    }

    store.dataChanges.forEach((value, key) => {
      if (store.isSame(value, store.dataServer[key])) {
        store.dataChanges.delete(key);
      }
    });
  })();

  if (typeof store.options.afterSave === 'function') {
    await store.options.afterSave(store, updates, response);
  }

  return response.status;
}
async function processSaveResponse(store, updates, response) {
  store.options.log(`[${store.options.name}] Response received from server.`);

  if (response.status === 'error') {
    action(() => {
      let errorFields = [];
      if (response.error) {
        if (typeof response.error === 'string') {
          store.serverError = response.error;
        } else {
          Object.assign(store.dataErrors, response.error);
          errorFields = Object.keys(response.error);
        }
      }

      // Supports an array of field names in error_field or a string
      errorFields = errorFields.concat(response.error_field);
      errorFields.forEach((field) => {
        if (store.options.autoSaveInterval && !store.dataErrors[field] && store.isSame(updates[field], store.data[field])) {
          store.data[field] = store.dataServer[field]; // revert or it'll keep trying to autosave it
        }
        delete updates[field]; // don't save it as the new dataServer value
      });
    })();
  } else {
    store.serverError = null;
  }

  Object.assign(store.dataServer, updates);

  action(() => {
    if (response.data) {
      Object.assign(store.dataServer, response.data);
      Object.assign(store.data, response.data);
    }

    store.dataChanges.forEach((value, key) => {
      if (store.isSame(value, store.dataServer[key])) {
        store.dataChanges.delete(key);
      }
    });
  })();

  if (typeof store.options.afterSave === 'function') {
    await store.options.afterSave(store, updates, response);
  }

  return response.status;
}
async function processSaveResponse ( store , updates , response ) { store . options . log ( ` ${ store . options . name } ` ) ; if ( response . status === 'error' ) { action ( ( ) => { let errorFields = [ ] ; if ( response . error ) { if ( typeof response . error === 'string' ) { store . serverError = response . error ; } else { Object . assign ( store . dataErrors , response . error ) ; errorFields = Object . keys ( response . error ) ; } } // Supports an array of field names in error_field or a string errorFields = errorFields . concat ( response . error_field ) ; errorFields . forEach ( ( field ) => { if ( store . options . autoSaveInterval && ! store . dataErrors [ field ] && store . isSame ( updates [ field ] , store . data [ field ] ) ) { store . data [ field ] = store . dataServer [ field ] ; // revert or it'll keep trying to autosave it } delete updates [ field ] ; // don't save it as the new dataServer value } ) ; } ) ( ) ; } else { store . serverError = null ; } Object . assign ( store . dataServer , updates ) ; action ( ( ) => { if ( response . data ) { Object . assign ( store . dataServer , response . data ) ; Object . assign ( store . data , response . data ) ; } store . dataChanges . forEach ( ( value , key ) => { if ( store . isSame ( value , store . dataServer [ key ] ) ) { store . dataChanges . delete ( key ) ; } } ) ; } ) ( ) ; if ( typeof store . options . afterSave === 'function' ) { await store . options . afterSave ( store , updates , response ) ; } return response . status ; }
Records successfully saved data as saved
and reverts fields server indicates to be in error
@param {FormStore} store
@param {Object} updates - what we sent to the server
@param {Object} response
@param {String} [response.data] - optional updated data to merge into the store (server.create can return id here)
@param {String} [response.status] - 'error' indicates one or more fields were invalid and not saved.
@param {String|Object} [response.error] - either a single error message to show to user if string or field-specific error messages if object
@param {String|Array} [response.error_field] - name of the field (or array of field names) in error
If autoSave is enabled, any field in error_field for which there is no error message in response.error will be reverted
to prevent autoSave from endlessly trying to save the changed field.
@returns response.status
Records successfully saved data as saved and reverts fields server indicates to be in error