Skip to content

Commit 984927a

Browse files
committed
fix: fix regression on duplicate component init when using shared data objects
fix #7805
1 parent cf0b1b7 commit 984927a

File tree

3 files changed

+38
-22
lines changed

3 files changed

+38
-22
lines changed

src/core/vdom/create-component.js

+6-17
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import {
3232
renderRecyclableComponentTemplate
3333
} from 'weex/runtime/recycle-list/render-component-template'
3434

35-
// hooks to be invoked on component VNodes during patch
35+
// inline hooks to be invoked on component VNodes during patch
3636
const componentVNodeHooks = {
3737
init (
3838
vnode: VNodeWithData,
@@ -189,8 +189,8 @@ export function createComponent (
189189
}
190190
}
191191

192-
// merge component management hooks onto the placeholder node
193-
mergeHooks(data)
192+
// install component management hooks onto the placeholder node
193+
installComponentHooks(data)
194194

195195
// return a placeholder vnode
196196
const name = Ctor.options.name || tag
@@ -234,22 +234,11 @@ export function createComponentInstanceForVnode (
234234
return new vnode.componentOptions.Ctor(options)
235235
}
236236

237-
function mergeHooks (data: VNodeData) {
238-
if (!data.hook) {
239-
data.hook = {}
240-
}
237+
function installComponentHooks (data: VNodeData) {
238+
const hooks = data.hook || (data.hook = {})
241239
for (let i = 0; i < hooksToMerge.length; i++) {
242240
const key = hooksToMerge[i]
243-
const fromParent = data.hook[key]
244-
const ours = componentVNodeHooks[key]
245-
data.hook[key] = fromParent ? mergeHook(ours, fromParent) : ours
246-
}
247-
}
248-
249-
function mergeHook (one: Function, two: Function): Function {
250-
return function (a, b, c, d) {
251-
one(a, b, c, d)
252-
two(a, b, c, d)
241+
hooks[key] = componentVNodeHooks[key]
253242
}
254243
}
255244

test/unit/modules/vdom/create-component.spec.js

-5
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,10 @@ describe('create-component', () => {
1919
props: ['msg'],
2020
render () {}
2121
}
22-
const init = jasmine.createSpy()
2322
const data = {
2423
props: { msg: 'hello world' },
2524
attrs: { id: 1 },
2625
staticAttrs: { class: 'foo' },
27-
hook: { init },
2826
on: { notify: 'onNotify' }
2927
}
3028
const vnode = createComponent(child, data, vm, vm)
@@ -38,9 +36,6 @@ describe('create-component', () => {
3836
expect(vnode.elm).toBeUndefined()
3937
expect(vnode.ns).toBeUndefined()
4038
expect(vnode.context).toEqual(vm)
41-
42-
vnode.data.hook.init(vnode)
43-
expect(init.calls.argsFor(0)[0]).toBe(vnode)
4439
})
4540

4641
it('create a component when resolved with async loading', done => {

test/unit/modules/vdom/patch/edge-cases.spec.js

+32
Original file line numberDiff line numberDiff line change
@@ -374,4 +374,36 @@ describe('vdom patch: edge cases', () => {
374374
expect(vm.$el.querySelector('custom-foo').getAttribute('selected')).toBe('1')
375375
Vue.config.ignoredElements = []
376376
})
377+
378+
// #7805
379+
it('should not cause duplicate init when components share data object', () => {
380+
const Base = {
381+
render (h) {
382+
return h('div', this.$options.name)
383+
}
384+
}
385+
386+
const Foo = {
387+
name: 'Foo',
388+
extends: Base
389+
}
390+
391+
const Bar = {
392+
name: 'Bar',
393+
extends: Base
394+
}
395+
396+
const vm = new Vue({
397+
render (h) {
398+
const data = { staticClass: 'text-red' }
399+
400+
return h('div', [
401+
h(Foo, data),
402+
h(Bar, data)
403+
])
404+
}
405+
}).$mount()
406+
407+
expect(vm.$el.textContent).toBe('FooBar')
408+
})
377409
})

0 commit comments

Comments
 (0)