Skip to content

Commit 084a86c

Browse files
committed
simplifies AposSchema, improves perfs
1 parent 5a8bffd commit 084a86c

2 files changed

Lines changed: 18 additions & 46 deletions

File tree

modules/@apostrophecms/schema/ui/apos/components/AposSchema.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
:modifiers="fields[field.name].modifiers"
5050
:display-options="getDisplayOptions(field.name)"
5151
:trigger-validation="triggerValidation"
52-
:server-error="fields[field.name].serverError"
52+
:server-error="serverErrors[field.name]"
5353
:doc-id="docId"
5454
:generation="generation"
5555
@update-doc-data="onUpdateDocData"

modules/@apostrophecms/schema/ui/apos/logic/AposSchema.js

Lines changed: 17 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { detectFieldChange } from 'Modules/@apostrophecms/schema/lib/detectChange';
21
import { getConditionTypesObject } from '../lib/conditionalFields';
2+
import debounce from 'lodash/debounce';
33

44
export default {
55
name: 'AposSchema',
@@ -112,7 +112,8 @@ export default {
112112
fieldErrors: {}
113113
},
114114
fieldState: {},
115-
fieldComponentMap: window.apos.schema.components.fields || {}
115+
fieldComponentMap: window.apos.schema.components.fields || {},
116+
updateNextAndEmitDebounced: debounce(this.updateNextAndEmit, 50)
116117
};
117118
},
118119
computed: {
@@ -130,10 +131,6 @@ export default {
130131
...item,
131132
required
132133
},
133-
value: {
134-
data: this.modelValue.data[item.name]
135-
},
136-
serverError: this.serverErrors && this.serverErrors[item.name],
137134
modifiers: this.computeModifiers(item)
138135
}
139136
};
@@ -166,7 +163,17 @@ export default {
166163
return compareMetaState;
167164
}
168165
},
166+
beforeUnmount() {
167+
this.updateNextAndEmitDebounced.cancel();
168+
},
169169
watch: {
170+
fieldState: {
171+
deep: 2,
172+
handler() {
173+
this.updateNextAndEmitDebounced();
174+
},
175+
flush: 'post'
176+
},
170177
schema() {
171178
this.populateDocData();
172179
},
@@ -211,6 +218,7 @@ export default {
211218
created() {
212219
this.populateDocData();
213220
},
221+
214222
methods: {
215223
emitValidate() {
216224
this.$emit('validate');
@@ -269,53 +277,17 @@ export default {
269277
if (!this.schemaReady) {
270278
return;
271279
}
272-
const oldHasErrors = this.next.hasErrors;
273-
// destructure these for non-linked comparison
274-
const oldFieldState = { ...this.next.fieldState };
275-
const newFieldState = { ...this.fieldState };
276-
277-
let changeFound = false;
278-
279-
this.next.hasErrors = false;
280-
this.next.fieldState = { ...this.fieldState };
281-
282280
this.schema
283281
.filter(field => this.displayComponent(field))
284282
.forEach(field => {
285283
if (this.fieldState[field.name].error) {
286284
this.next.hasErrors = true;
287285
}
288-
// This simply check if a field has changed since it has been
289-
// instantiated
290-
if (
291-
this.fieldState[field.name].data !== undefined &&
292-
detectFieldChange(
293-
field,
294-
this.next.data[field.name],
295-
this.fieldState[field.name].data
296-
)
297-
) {
298-
changeFound = true;
299-
300-
// fieldState never gets the relationships postprocessed data
301-
// that's why it gets seen as different than next all the time
302-
this.next.data[field.name] = this.fieldState[field.name].data;
303-
} else {
304-
this.next.data[field.name] = this.modelValue.data[field.name];
305-
}
286+
this.next.data[field.name] = this.fieldState[field.name].data;
306287
});
307-
if (
308-
oldHasErrors !== this.next.hasErrors ||
309-
oldFieldState !== newFieldState
310-
) {
311-
// Otherwise the save button may never unlock
312-
changeFound = true;
313-
}
314288

315-
if (changeFound) {
316-
// ... removes need for deep watch at parent level
317-
this.$emit('update:model-value', { ...this.next });
318-
}
289+
this.next.fieldState = { ...this.fieldState };
290+
this.$emit('update:model-value', this.next);
319291
},
320292
displayComponent({ name, hidden = false }) {
321293
if (hidden === true) {

0 commit comments

Comments
 (0)