@@ -7,15 +7,20 @@ 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 ;
10
+ /** Simple check which fields are used */
11
+ function shouldUpdate ( template : string , computedField : string , val : Record < string , any > , oldVal : Record < string , any > ) {
12
+ for ( const key of Object . keys ( val ) ) {
13
+ if (
14
+ key !== computedField &&
15
+ checkFieldInTemplate ( template , key ) &&
16
+ val [ key ] !== oldVal [ key ] &&
17
+ JSON . stringify ( val [ key ] ) !== JSON . stringify ( oldVal [ key ] )
18
+ ) {
19
+ return true ;
20
+ }
16
21
}
17
- return true ;
18
- } ;
22
+ return false ;
23
+ }
19
24
20
25
export const useCollectionRelations = ( collection : string ) : Ref < any [ ] > => {
21
26
const { useRelationsStore } = useStores ( ) ;
@@ -33,18 +38,24 @@ export const useDeepValues = (
33
38
values : Ref < Record < string , any > > ,
34
39
relations : Ref < any [ ] > ,
35
40
collection : string ,
41
+ computedField : string ,
36
42
pk : string ,
37
43
template : string
38
44
) => {
39
45
const api = useApi ( ) ;
40
- const finalValues = ref < Record < string , any > > ( values . value ) ;
41
- watch ( values , async ( val , oldVal ) => {
46
+ const finalValues = ref < Record < string , any > > ( { } ) ;
47
+ watch ( values , async ( ) => {
48
+ if ( ! shouldUpdate ( template , computedField , values . value , finalValues . value ) ) {
49
+ finalValues . value = values . value ;
50
+ return ;
51
+ }
52
+
42
53
const relationalData : Record < string , any > = { } ;
43
54
44
55
for ( const key of Object . keys ( values . value ) ) {
45
56
const relation = relations . value . find ( ( rel ) => rel . meta ?. one_field === key ) ;
46
57
47
- if ( ! relation || ! checkFieldInTemplate ( template , key ) || ! checkFieldNeedsUpdate ( key , val , oldVal ) ) {
58
+ if ( ! relation ) {
48
59
continue ;
49
60
}
50
61
@@ -98,9 +109,7 @@ export const useDeepValues = (
98
109
relationalData [ key ] = arrayOfData ;
99
110
}
100
111
101
- if ( Object . keys ( relationalData ) . length ) {
102
- finalValues . value = { ...values . value , ...relationalData } ;
103
- }
112
+ finalValues . value = { ...values . value , ...relationalData } ;
104
113
} ) ;
105
114
106
115
return finalValues ;
0 commit comments