Skip to content

Commit b58db14

Browse files
committed
allow wait-for to fire sync inside compiled hooks (fix vuejs#965)
1 parent a01f056 commit b58db14

File tree

2 files changed

+64
-29
lines changed

2 files changed

+64
-29
lines changed

src/directives/component.js

Lines changed: 47 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ module.exports = {
2828
// cache object, with its constructor id as the key.
2929
this.keepAlive = this._checkParam('keep-alive') != null
3030
// wait for event before insertion
31-
this.readyEvent = this._checkParam('wait-for')
31+
this.waitForEvent = this._checkParam('wait-for')
3232
// check ref
3333
this.refID = this._checkParam(config.prefix + 'ref')
3434
if (this.keepAlive) {
@@ -66,15 +66,23 @@ module.exports = {
6666
*/
6767

6868
initStatic: function () {
69-
var child = this.build()
69+
// wait-for
7070
var anchor = this.anchor
71+
var options
72+
var waitFor = this.waitForEvent
73+
if (waitFor) {
74+
options = {
75+
created: function () {
76+
this.$once(waitFor, function () {
77+
this.$before(anchor)
78+
})
79+
}
80+
}
81+
}
82+
var child = this.build(options)
7183
this.setCurrent(child)
72-
if (!this.readyEvent) {
84+
if (!this.waitForEvent) {
7385
child.$before(anchor)
74-
} else {
75-
child.$once(this.readyEvent, function () {
76-
child.$before(anchor)
77-
})
7886
}
7987
},
8088

@@ -93,32 +101,38 @@ module.exports = {
93101
* specified transition mode. Accepts a few additional
94102
* arguments specifically for vue-router.
95103
*
104+
* The callback is called when the full transition is
105+
* finished.
106+
*
96107
* @param {String} value
97-
* @param {Object} data
98-
* @param {Function} afterBuild
99-
* @param {Function} afterTransition
108+
* @param {Function} [cb]
100109
*/
101110

102-
setComponent: function (value, data, afterBuild, afterTransition) {
111+
setComponent: function (value, cb) {
103112
this.invalidatePending()
104113
if (!value) {
105114
// just remove current
106115
this.unbuild(true)
107-
this.remove(this.childVM, afterTransition)
116+
this.remove(this.childVM, cb)
108117
this.unsetCurrent()
109118
} else {
110119
this.resolveComponent(value, _.bind(function () {
111120
this.unbuild(true)
112-
var newComponent = this.build(data)
113-
/* istanbul ignore if */
114-
if (afterBuild) afterBuild(newComponent)
121+
var options
115122
var self = this
116-
if (this.readyEvent) {
117-
newComponent.$once(this.readyEvent, function () {
118-
self.transition(newComponent, afterTransition)
119-
})
120-
} else {
121-
this.transition(newComponent, afterTransition)
123+
var waitFor = this.waitForEvent
124+
if (waitFor) {
125+
options = {
126+
created: function () {
127+
this.$once(waitFor, function () {
128+
self.transition(this, cb)
129+
})
130+
}
131+
}
132+
}
133+
var newComponent = this.build(options)
134+
if (!waitFor) {
135+
this.transition(newComponent, cb)
122136
}
123137
}, this))
124138
}
@@ -157,31 +171,35 @@ module.exports = {
157171
* If keep alive and has cached instance, insert that
158172
* instance; otherwise build a new one and cache it.
159173
*
160-
* @param {Object} [data]
174+
* @param {Object} [extraOptions]
161175
* @return {Vue} - the created instance
162176
*/
163177

164-
build: function (data) {
178+
build: function (extraOptions) {
165179
if (this.keepAlive) {
166180
var cached = this.cache[this.componentID]
167181
if (cached) {
168182
return cached
169183
}
170184
}
171185
if (this.Component) {
172-
var parent = this._host || this.vm
173-
var el = templateParser.clone(this.el)
174-
var child = parent.$addChild({
175-
el: el,
176-
data: data,
186+
// default options
187+
var options = {
188+
el: templateParser.clone(this.el),
177189
template: this.template,
178190
// if no inline-template, then the compiled
179191
// linker can be cached for better performance.
180192
_linkerCachable: !this.template,
181193
_asComponent: true,
182194
_isRouterView: this._isRouterView,
183195
_context: this.vm
184-
}, this.Component)
196+
}
197+
// extra options
198+
if (extraOptions) {
199+
_.extend(options, extraOptions)
200+
}
201+
var parent = this._host || this.vm
202+
var child = parent.$addChild(options, this.Component)
185203
if (this.keepAlive) {
186204
this.cache[this.componentID] = child
187205
}

test/unit/specs/directives/component_spec.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,23 @@ if (_.inBrowser) {
278278
expect(el.textContent).toBe('AAA')
279279
})
280280

281+
it('sync wait-for inside compiled hook', function () {
282+
new Vue({
283+
el: el,
284+
template: '<view-a wait-for="ok"></view-a>',
285+
components: {
286+
'view-a': {
287+
template: 'AAA',
288+
compiled: function () {
289+
expect(el.textContent).toBe('')
290+
this.$emit('ok')
291+
}
292+
}
293+
}
294+
})
295+
expect(el.textContent).toBe('AAA')
296+
})
297+
281298
it('wait-for for dynamic components', function (done) {
282299
var vm = new Vue({
283300
el: el,

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy