File tree Expand file tree Collapse file tree 2 files changed +39
-5
lines changed
test/unit/features/instance Expand file tree Collapse file tree 2 files changed +39
-5
lines changed Original file line number Diff line number Diff line change @@ -247,16 +247,20 @@ function initWatch (vm: Component, watch: Object) {
247247 }
248248}
249249
250- function createWatcher ( vm : Component , key : string , handler : any ) {
251- let options
250+ function createWatcher (
251+ vm : Component ,
252+ keyOrFn : string | Function ,
253+ handler : any ,
254+ options ?: Object
255+ ) {
252256 if ( isPlainObject ( handler ) ) {
253257 options = handler
254258 handler = handler . handler
255259 }
256260 if ( typeof handler === 'string' ) {
257261 handler = vm [ handler ]
258262 }
259- vm . $watch ( key , handler , options )
263+ return vm . $watch ( keyOrFn , handler , options )
260264}
261265
262266export function stateMixin ( Vue : Class < Component > ) {
@@ -287,10 +291,13 @@ export function stateMixin (Vue: Class<Component>) {
287291
288292 Vue . prototype . $watch = function (
289293 expOrFn : string | Function ,
290- cb : Function ,
294+ cb : any ,
291295 options ?: Object
292296 ) : Function {
293297 const vm : Component = this
298+ if ( isPlainObject ( cb ) ) {
299+ return createWatcher ( vm , expOrFn , cb , options )
300+ }
294301 options = options || { }
295302 options . user = true
296303 const watcher = new Watcher ( vm , expOrFn , cb , options )
Original file line number Diff line number Diff line change @@ -21,14 +21,17 @@ describe('Instance methods data', () => {
2121 describe ( '$watch' , ( ) => {
2222 let vm , spy
2323 beforeEach ( ( ) => {
24+ spy = jasmine . createSpy ( 'watch' )
2425 vm = new Vue ( {
2526 data : {
2627 a : {
2728 b : 1
2829 }
30+ } ,
31+ methods : {
32+ foo : spy
2933 }
3034 } )
31- spy = jasmine . createSpy ( 'watch' )
3235 } )
3336
3437 it ( 'basic usage' , done => {
@@ -81,6 +84,30 @@ describe('Instance methods data', () => {
8184 } ) . then ( done )
8285 } )
8386
87+ it ( 'handler option' , done => {
88+ var oldA = vm . a
89+ vm . $watch ( 'a' , {
90+ handler : spy ,
91+ deep : true
92+ } )
93+ vm . a . b = 2
94+ waitForUpdate ( ( ) => {
95+ expect ( spy ) . toHaveBeenCalledWith ( oldA , oldA )
96+ vm . a = { b : 3 }
97+ } ) . then ( ( ) => {
98+ expect ( spy ) . toHaveBeenCalledWith ( vm . a , oldA )
99+ } ) . then ( done )
100+ } )
101+
102+ it ( 'handler option in string' , ( ) => {
103+ vm . $watch ( 'a.b' , {
104+ handler : 'foo' ,
105+ immediate : true
106+ } )
107+ expect ( spy . calls . count ( ) ) . toBe ( 1 )
108+ expect ( spy ) . toHaveBeenCalledWith ( 1 )
109+ } )
110+
84111 it ( 'warn expresssion' , ( ) => {
85112 vm . $watch ( 'a + b' , spy )
86113 expect ( 'Watcher only accepts simple dot-delimited paths' ) . toHaveBeenWarned ( )
You can’t perform that action at this time.
0 commit comments