1- import { detectFieldChange } from 'Modules/@apostrophecms/schema/lib/detectChange' ;
21import { getConditionTypesObject } from '../lib/conditionalFields' ;
2+ import debounce from 'lodash/debounce' ;
33
44export 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