8000 handle multiple activate hooks from mixin (fix #2305) · codeclever/vue@c63d6a3 · GitHub
[go: up one dir, main page]

Skip to content

Commit c63d6a3

Browse files
committed
handle multiple activate hooks from mixin (fix vuejs#2305)
1 parent 8419d8c commit c63d6a3

File tree

3 files changed

+55
-4
lines changed

3 files changed

+55
-4
lines changed

src/directives/internal/component.js

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,12 +139,12 @@ export default {
139139
// actual mount
140140
this.unbuild(true)
141141
var self = this
142-
var activateHook = this.Component.options.activate
142+
var activateHooks = this.Component.options.activate
143143
var cached = this.getCached()
144144
var newComponent = this.build()
145-
if (activateHook && !cached) {
145+
if (activateHooks && !cached) {
146146
this.waitingFor = newComponent
147-
activateHook.call(newComponent, function () {
147+
callActivateHooks(activateHooks, newComponent, function () {
148148
if (self.waitingFor !== newComponent) {
149149
return
150150
}
@@ -358,3 +358,24 @@ export default {
358358
}
359359
}
360360
}
361+
362+
/**
363+
* Call activate hooks in order (asynchronous)
364+
*
365+
* @param {Array} hooks
366+
* @param {Vue} vm
367+
* @param {Function} cb
368+
*/
369+
370+
function callActivateHooks (hooks, vm, cb) {
371+
var total = hooks.length
372+
var called = 0
373+
hooks[0].call(vm, next)
374+
function next () {
375+
if (++called >= total) {
376+
cb()
377+
} else {
378+
hooks[called].call(vm, next)
379+
}
380+
}
381+
}

src/util/options.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,8 @@ strats.detached =
126126
strats.beforeCompile =
127127
strats.compiled =
128128
strats.beforeDestroy =
129-
strats.destroyed = function (parentVal, childVal) {
129+
strats.destroyed =
130+
strats.activate = function (parentVal, childVal) {
130131
return childVal
131132
? parentVal
132133
? parentVal.concat(childVal)

test/unit/specs/directives/internal/component_spec.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,35 @@ describe('Component', function () {
282282
})
283283
})
284284

285+
it('multiple activate hooks', function (done) {
286+
var mixinSpy = jasmine.createSpy('mixin activate')
287+
new Vue({
288+
el: el,
289+
template: '<view-a></view-a>',
290+
components: {
291+
'view-a': {
292+
template: 'AAA',
293+
mixins: [{
294+
activate: function (done) {
295+
expect(el.textContent).toBe('')
296+
mixinSpy()
297+
done()
298+
}
299+
}],
300+
activate: function (ready) {
301+
setTimeout(function () {
302+
expect(mixinSpy).toHaveBeenCalled()
303+
expect(el.textContent).toBe('')
304+
ready()
305+
expect(el.textContent).toBe('AAA')
306+
done()
307+
}, 0)
308+
}
309+
}
310+
}
311+
})
312+
})
313+
285314
it('activate hook for dynamic components', function (done) {
286315
var next
287316
var vm = new Vue({

0 commit comments

Comments
 (0)
0