File tree Expand file tree Collapse file tree 4 files changed +50
-6
lines changed Expand file tree Collapse file tree 4 files changed +50
-6
lines changed Original file line number Diff line number Diff line change @@ -109,9 +109,8 @@ export function lifecycleMixin (Vue: Class<Component>) {
109109 if ( vm . $vnode && vm . $parent && vm . $vnode === vm . $parent . _vnode ) {
110110 vm . $parent . $el = vm . $el
111111 }
112- if ( vm . _isMounted ) {
113- callHook ( vm , 'updated' )
114- }
112+ // updated hook is called by the scheduler to ensure that children are
113+ // updated in a parent's updated hook.
115114 }
116115
117116 Vue . prototype . _updateFromParent = function (
Original file line number Diff line number Diff line change 22
33import type Watcher from './watcher'
44import config from '../config'
5+ import { callHook } from '../instance/lifecycle'
56import {
67 warn ,
78 nextTick ,
@@ -32,6 +33,7 @@ function resetSchedulerState () {
3233 */
3334function flushSchedulerQueue ( ) {
3435 flushing = true
36+ let watcher , id , vm
3537
3638 // Sort queue before flush.
3739 // This ensures that:
@@ -46,8 +48,8 @@ function flushSchedulerQueue () {
4648 // do not cache length because more watchers might be pushed
4749 // as we run existing watchers
4850 for ( index = 0 ; index < queue . length ; index ++ ) {
49- const watcher = queue [ index ]
50- const id = watcher . id
51+ watcher = queue [ index ]
52+ id = watcher . id
5153 has [ id ] = null
5254 watcher . run ( )
5355 // in dev build, check and stop circular updates.
@@ -67,6 +69,16 @@ function flushSchedulerQueue () {
6769 }
6870 }
6971
72+ // call updated hooks
73+ index = queue . length
74+ while ( index -- ) {
75+ watcher = queue [ index ]
76+ vm = watcher . vm
77+ if ( vm . _watcher === watcher && vm . _isMounted ) {
78+ callHook ( vm , 'updated' )
79+ }
80+ }
81+
7082 // devtool hook
7183 /* istanbul ignore if */
7284 if ( devtools && config . devtools ) {
Original file line number Diff line number Diff line change @@ -156,6 +156,34 @@ describe('Options lifecyce hooks', () => {
156156 expect ( spy ) . toHaveBeenCalled ( )
157157 } ) . then ( done )
158158 } )
159+
160+ it ( 'should be called after children are updated' , done => {
161+ const calls = [ ]
162+ const vm = new Vue ( {
163+ template : '<div><test ref="child">{{ msg }}</test></div>' ,
164+ data : { msg : 'foo' } ,
165+ components : {
166+ test : {
167+ template : `<div><slot></slot></div>` ,
168+ updated ( ) {
169+ expect ( this . $el . textContent ) . toBe ( 'bar' )
170+ calls . push ( 'child' )
171+ }
172+ }
173+ } ,
174+ updated ( ) {
175+ expect ( this . $el . textContent ) . toBe ( 'bar' )
176+ calls . push ( 'parent' )
177+ }
178+ } ) . $mount ( )
179+
180+ expect ( calls ) . toEqual ( [ ] )
181+ vm . msg = 'bar'
182+ expect ( calls ) . toEqual ( [ ] )
183+ waitForUpdate ( ( ) => {
184+ expect ( calls ) . toEqual ( [ 'child' , 'parent' ] )
185+ } ) . then ( done )
186+ } )
159187 } )
160188
161189 describe ( 'beforeDestroy' , ( ) => {
Original file line number Diff line number Diff line change 11import Vue from 'vue'
22import config from 'core/config'
3- import { queueWatcher } from 'core/observer/scheduler'
3+ import { queueWatcher as _queueWatcher } from 'core/observer/scheduler'
4+
5+ function queueWatcher ( watcher ) {
6+ watcher . vm = { } // mock vm
7+ _queueWatcher ( watcher )
8+ }
49
510describe ( 'Scheduler' , ( ) => {
611 let spy
You can’t perform that action at this time.
0 commit comments