@@ -19,15 +19,21 @@ const AsyncComputed = {
19
19
. asyncComputed = Vue . config . optionMergeStrategies . computed
20
20
21
21
Vue . mixin ( {
22
+ data ( ) {
23
+ return {
24
+ _asyncComputed : { } ,
25
+ }
26
+ } ,
22
27
beforeCreate ( ) {
23
28
const optionData = this . $options . data
24
29
const asyncComputed = this . $options . asyncComputed || { }
25
- this . $asyncComputed = { }
26
-
27
- if ( ! Object . keys ( asyncComputed ) . length ) return
28
30
29
31
if ( ! this . $options . computed ) this . $options . computed = { }
30
32
33
+ this . $options . computed . $asyncComputed = ( ) => this . $data . _asyncComputed
34
+
35
+ if ( ! Object . keys ( asyncComputed ) . length ) return
36
+
31
37
for ( const key in asyncComputed ) {
32
38
const getter = getterFn ( key , this . $options . asyncComputed [ key ] )
33
39
this . $options . computed [ prefix + key ] = getter
@@ -74,17 +80,17 @@ const AsyncComputed = {
74
80
if ( ! newPromise || ! newPromise . then ) {
75
81
newPromise = Promise . resolve ( newPromise )
76
82
}
77
- setAsyncState ( this . $asyncComputed [ key ] , 'updating' )
83
+ setAsyncState ( this , key , 'updating' )
78
84
79
85
newPromise . then ( value => {
80
86
if ( thisPromise !== promiseId ) return
81
- setAsyncState ( this . $asyncComputed [ key ] , 'success' )
87
+ setAsyncState ( this , key , 'success' )
82
88
this [ key ] = value
83
89
} ) . catch ( err => {
84
90
if ( thisPromise !== promiseId ) return
85
91
86
- setAsyncState ( this . $asyncComputed [ key ] , 'error' )
87
- this . $asyncComputed [ key ] . exception = err
92
+ setAsyncState ( this , key , 'error' )
93
+ Vue . set ( this . $data . _asyncComputed [ key ] , ' exception' , err )
88
94
if ( pluginOptions . errorHandler === false ) return
89
95
90
96
const handler = ( pluginOptions . errorHandler === undefined )
@@ -98,25 +104,25 @@ const AsyncComputed = {
98
104
}
99
105
} )
100
106
}
101
- this . $asyncComputed [ key ] = {
107
+ Vue . set ( this . $data . _asyncComputed , key , {
102
108
exception : null ,
103
109
update : ( ) => {
104
110
watcher ( getterOnly ( this . $options . asyncComputed [ key ] ) ( ) )
105
111
}
106
- }
107
- setAsyncState ( this . $asyncComputed [ key ] , 'updating' )
112
+ } )
113
+ setAsyncState ( this , key , 'updating' )
108
114
this . $watch ( prefix + key , watcher , { immediate : true } )
109
115
}
110
116
}
111
117
} )
112
118
}
113
119
}
114
120
115
- function setAsyncState ( stateObject , state ) {
116
- stateObject . state = state
117
- stateObject . updating = state === 'updating'
118
- stateObject . error = state === 'error'
119
- stateObject . success = state === 'success'
121
+ function setAsyncState ( vm , stateObject , state ) {
122
+ vm . $set ( vm . $data . _asyncComputed [ stateObject ] , ' state' , state )
123
+ vm . $set ( vm . $data . _asyncComputed [ stateObject ] , ' updating' , state === 'updating' )
124
+ vm . $set ( vm . $data . _asyncComputed [ stateObject ] , ' error' , state === 'error' )
125
+ vm . $set ( vm . $data . _asyncComputed [ stateObject ] , ' success' , state === 'success' )
120
126
}
121
127
122
128
function getterOnly ( fn ) {
0 commit comments