@@ -7,6 +7,16 @@ export function checkFieldInTemplate(template: string, field: string) {
7
7
return ( matches || [ ] ) . some ( ( m ) => m . includes ( field ) ) ;
8
8
}
9
9
10
+ const checkFieldNeedsUpdate = ( field : string , newVal : Record < string , any > , oldVal : Record < string , any > ) => {
11
+ if ( oldVal [ field ] === newVal [ field ] ) {
12
+ return false ;
13
+ }
14
+ if ( JSON . stringify ( oldVal [ field ] ) === JSON . stringify ( newVal [ field ] ) ) {
15
+ return false ;
16
+ }
17
+ return true ;
18
+ } ;
19
+
10
20
export const useCollectionRelations = ( collection : string ) : Ref < any [ ] > => {
11
21
const { useRelationsStore } = useStores ( ) ;
12
22
const { getRelationsForCollection } = useRelationsStore ( ) ;
@@ -28,13 +38,13 @@ export const useDeepValues = (
28
38
) => {
29
39
const api = useApi ( ) ;
30
40
const finalValues = ref < Record < string , any > > ( values . value ) ;
31
- watch ( values , async ( ) => {
41
+ watch ( values , async ( val , oldVal ) => {
32
42
const relationalData : Record < string , any > = { } ;
33
43
34
44
for ( const key of Object . keys ( values . value ) ) {
35
45
const relation = relations . value . find ( ( rel ) => rel . meta ?. one_field === key ) ;
36
46
37
- if ( ! relation || ! checkFieldInTemplate ( template , key ) ) {
47
+ if ( ! relation || ! checkFieldInTemplate ( template , key ) || ! checkFieldNeedsUpdate ( key , val , oldVal ) ) {
38
48
continue ;
39
49
}
40
50
@@ -88,7 +98,9 @@ export const useDeepValues = (
88
98
relationalData [ key ] = arrayOfData ;
89
99
}
90
100
91
- finalValues . value = { ...values . value , ...relationalData } ;
101
+ if ( Object . keys ( relationalData ) . length ) {
102
+ finalValues . value = { ...values . value , ...relationalData } ;
103
+ }
92
104
} ) ;
93
105
94
106
return finalValues ;
0 commit comments