File tree Expand file tree Collapse file tree 2 files changed +49
-1
lines changed Expand file tree Collapse file tree 2 files changed +49
-1
lines changed Original file line number Diff line number Diff line change @@ -101,7 +101,7 @@ const AsyncComputed = {
101
101
this . $asyncComputed [ key ] = {
102
102
exception : null ,
103
103
update : ( ) => {
104
- watcher ( getterOnly ( this . $options . asyncComputed [ key ] ) ( ) )
104
+ watcher ( getterOnly ( this . $options . asyncComputed [ key ] ) . apply ( this ) )
105
105
}
106
106
}
107
107
setAsyncState ( this . $asyncComputed [ key ] , 'updating' )
Original file line number Diff line number Diff line change @@ -809,6 +809,54 @@ test("$asyncComputed[name].update triggers re-evaluation", t => {
809
809
} )
810
810
} )
811
811
812
+ test ( "$asyncComputed[name].update has the correct execution context" , t => {
813
+ t . plan ( 8 )
814
+ let addedValue = 1
815
+ const vm = new Vue ( {
816
+ data ( ) {
817
+ return {
818
+ valueToReturn : 1 ,
819
+ }
820
+ } ,
821
+ asyncComputed : {
822
+ a ( ) {
823
+ return new Promise ( resolve => {
824
+ resolve ( this . valueToReturn + addedValue )
825
+ } )
826
+ } ,
827
+ b : {
828
+ get ( ) {
829
+ return new Promise ( resolve => {
830
+ resolve ( this . valueToReturn + addedValue )
831
+ } )
832
+ } ,
833
+ } ,
834
+ } ,
835
+ } )
836
+
837
+ Vue . nextTick ( ( ) => {
838
+ // case 1: a is a function
839
+ t . equal ( vm . a , 2 )
840
+ t . equal ( vm . $asyncComputed [ 'a' ] . state , 'success' )
841
+ // case 2: b is an object with a getter function
842
+ t . equal ( vm . b , 2 )
843
+ t . equal ( vm . $asyncComputed [ 'b' ] . state , 'success' )
844
+
845
+ addedValue = 4
846
+
847
+ vm . $asyncComputed [ 'a' ] . update ( )
848
+ t . equal ( vm . $asyncComputed [ 'a' ] . state , 'updating' )
849
+
850
+ vm . $asyncComputed [ 'b' ] . update ( )
851
+ t . equal ( vm . $asyncComputed [ 'b' ] . state , 'updating' )
852
+
853
+ Vue . nextTick ( ( ) => {
854
+ t . equal ( vm . a , 5 )
855
+ t . equal ( vm . b , 5 )
856
+ } )
857
+ } )
858
+ } )
859
+
812
860
test ( "Plain components with neither `data` nor `asyncComputed` still work (issue #50)" , t => {
813
861
t . plan ( 1 )
814
862
const vm = new Vue ( {
You can’t perform that action at this time.
0 commit comments