Zotero.DataObject updates for changedData fields

changedData is the new approach to updating DataObject data, storing the
new values in a separate object until save so that they don't actually
change the object unless the save goes through.

- Add DataObject._getLatestField() to return either the changed value or
  the last-saved value
- Clarify that _markFieldChange() takes different values depending on
  whether the field uses 'changed' or 'changedData'
- Fix bug calculating hasChanged() when a 'changedData' value is a
  boolean
This commit is contained in:
Dan Stillman 2021-01-12 04:38:25 -05:00
parent 22addb3afd
commit 5a4d78578b

View file

@ -707,19 +707,26 @@ Zotero.DataObject.prototype._markAllDataTypeLoadStates = function (loaded) {
} }
} }
/**
* Get either the unsaved value of a field or the saved value if unchanged since the last save
*/
Zotero.DataObject.prototype._getLatestField = function (field) {
return this._changedData[field] !== undefined ? this._changedData[field] : this['_' + field];
};
/** /**
* Save old version of data that's being changed, to pass to the notifier * Save old version of data that's being changed, to pass to the notifier
* @param {String} field * @param {String} field
* @param {} oldValue * @param {} value - Old value for old-style 'changed' fields, and new value for 'changedData' fields
*/ */
Zotero.DataObject.prototype._markFieldChange = function (field, oldValue) { Zotero.DataObject.prototype._markFieldChange = function (field, value) {
// New method (changedData) // New method (changedData)
if (field == 'tags') { if (field == 'tags') {
if (Array.isArray(oldValue)) { if (Array.isArray(value)) {
this._changedData[field] = [...oldValue]; this._changedData[field] = [...value];
} }
else { else {
this._changedData[field] = oldValue; this._changedData[field] = value;
} }
return; return;
} }
@ -728,21 +735,19 @@ Zotero.DataObject.prototype._markFieldChange = function (field, oldValue) {
if (!this.id || this._previousData[field] !== undefined) { if (!this.id || this._previousData[field] !== undefined) {
return; return;
} }
if (Array.isArray(oldValue)) { if (Array.isArray(value)) {
this._previousData[field] = []; this._previousData[field] = [];
Object.assign(this._previousData[field], oldValue) Object.assign(this._previousData[field], value)
} }
else { else {
this._previousData[field] = oldValue; this._previousData[field] = value;
} }
} }
Zotero.DataObject.prototype.hasChanged = function() { Zotero.DataObject.prototype.hasChanged = function() {
var changed = Object.keys(this._changed).filter(dataType => this._changed[dataType]) var changed = Object.keys(this._changed).filter(dataType => this._changed[dataType])
.concat( .concat(Object.keys(this._changedData));
Object.keys(this._changedData).filter(dataType => this._changedData[dataType])
);
if (changed.length == 1 if (changed.length == 1
&& changed[0] == 'primaryData' && changed[0] == 'primaryData'
&& Object.keys(this._changed.primaryData).length == 1 && Object.keys(this._changed.primaryData).length == 1