8000 lifecycle api: deprecate $addChild() in favor of `parent` option · justnull/vue@74a6b01 · GitHub
[go: up one dir, main page]

Skip to content

Commit 74a6b01

Browse files
committed
lifecycle api: deprecate $addChild() in favor of parent option
1 parent 4464a91 commit 74a6b01

File tree

11 files changed

+44
-81
lines changed

11 files changed

+44
-81
lines changed

src/api/child.js

Lines changed: 0 additions & 20 deletions
This file was deleted.

src/directives/internal/component.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,10 @@ module.exports = {
174174
var options = {
175175
el: templateParser.clone(this.el),
176176
template: this.inlineTemplate,
177+
// make sure to add the child with correct parent
178+
// if this is a transcluded component, its parent
179+
// should be the transclusion host.
180+
parent: this._host || this.vm,
177181
// if no inline-template, then the compiled
178182
// linker can be cached for better performance.
179183
_linkerCachable: !this.inlineTemplate,
@@ -201,8 +205,7 @@ module.exports = {
201205
if (extraOptions) {
202206
_.extend(options, extraOptions)
203207
}
204-
var parent = this._host || this.vm
205-
var child = parent.$addChild(options, this.Component)
208+
var child = new this.Component(options)
206209
if (this.keepAlive) {
207210
this.cache[this.Component.cid] = child
208211
}

src/instance/init.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@ exports._init = function (options) {
1616
options = options || {}
1717

1818
this.$el = null
19-
this.$parent = options._parent
20-
this.$root = options._root || this
19+
this.$parent = options.parent
20+
this.$root = this.$parent
21+
? this.$parent.$root
22+
: this
2123
this.$children = []
2224
this.$refs = {} // child vm references
2325
this.$els = {} // element references
@@ -49,7 +51,7 @@ exports._init = function (options) {
4951
// if this is a transcluded component, context
5052
// will be the common parent vm of this instance
5153
// and its host.
52-
this._context = options._context || options._parent
54+
this._context = options._context || this.$parent
5355

5456
// scope:
5557
// if this is inside an inline v-for, the scope

src/vue.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ extend(p, require('./instance/misc'))
8383
extend(p, require('./api/data'))
8484
extend(p, require('./api/dom'))
8585
extend(p, require('./api/events'))
86-
extend(p, require('./api/child'))
8786
extend(p, require('./api/lifecycle'))
8887

8988
Vue.version = '1.0.0-beta.3'

test/unit/specs/api/child_spec.js

Lines changed: 0 additions & 26 deletions
This file was deleted.

test/unit/specs/api/events_spec.js

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@ describe('Events API', function () {
5959
})
6060

6161
it('$broadcast', function () {
62-
var child1 = vm.$addChild()
63-
var child2 = vm.$addChild()
64-
var child3 = child1.$addChild()
62+
var child1 = new Vue({ parent: vm })
63+
var child2 = new Vue({ parent: vm })
64+
var child3 = new Vue({ parent: child1 })
6565
child1.$on('test', spy)
6666
child2.$on('test', spy)
6767
child3.$on('test', spy)
@@ -70,9 +70,9 @@ describe('Events API', function () {
7070
})
7171

7272
it('$broadcast with propagation', function () {
73-
var child1 = vm.$addChild()
74-
var child2 = vm.$addChild()
75-
var child3 = child1.$addChild()
73+
var child1 = new Vue({ parent: vm })
74+
var child2 = new Vue({ parent: vm })
75+
var child3 = new Vue({ parent: child1 })
7676
child1.$on('test', function () {
7777
spy()
7878
return true
@@ -84,8 +84,8 @@ describe('Events API', function () {
8484
})
8585

8686
it('$broadcast optimization', function () {
87-
var child = vm.$addChild()
88-
var child2 = child.$addChild()
87+
var child = new Vue({ parent: vm })
88+
var child2 = new Vue({ parent: child })
8989
// hooks should not incurr the bookkeeping cost
9090
child.$on('hook:created', function () {})
9191
expect(vm._eventsCount['hook:created']).toBeUndefined()
@@ -121,8 +121,8 @@ describe('Events API', function () {
121121
})
122122

123123
it('$broadcast cancel', function () {
124-
var child = vm.$addChild()
125-
var child2 = child.$addChild()
124+
var child = new Vue({ parent: vm })
125+
var child2 = new Vue({ parent: child })
126126
child.$on('test', function () {
127127
return false
128128
})
@@ -132,8 +132,8 @@ describe('Events API', function () {
132132
})
133133

134134
it('$dispatch', function () {
135-
var child = vm.$addChild()
136-
var child2 = child.$addChild()
135+
var child = new Vue({ parent: vm })
136+
var child2 = new Vue({ parent: child })
137137
child2.$on('test', spy)
138138
child.$on('test', spy)
139139
vm.$on('test', spy)
@@ -142,9 +142,9 @@ describe('Events API', function () {
142142
})
143143

144144
it('$dispatch with propagation', function () {
145-
var child = vm.$addChild()
146-
var child2 = child.$addChild()
147-
var child3 = child2.$addChild()
145+
var child = new Vue({ parent: vm })
146+
var child2 = new Vue({ parent: child })
147+
var child3 = new Vue({ parent: child2 })
148148
child.$on('test', function () {
149149
spy()
150150
return true
@@ -155,8 +155,8 @@ describe('Events API', function () {
155155
})
156156

157157
it('$dispatch cancel', function () {
158-
var child = vm.$addChild()
159-
var child2 = child.$addChild()
158+
var child = new Vue({ parent: vm })
159+
var child2 = new Vue({ parent: child })
160160
child.$on('test', function () {
161161
return false
162162
})

test/unit/specs/api/lifecycle_spec.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,8 @@ if (_.inBrowser) {
196196

197197
it('parent', function () {
198198
var parent = new Vue()
199-
var child = parent.$addChild()
200-
var child2 = parent.$addChild()
199+
var child = new Vue({ parent: parent })
200+
var child2 = new Vue({ parent: parent })
201201
expect(parent.$children.length).toBe(2)
202202
child.$destroy()
203203
expect(parent.$children.length).toBe(1)
@@ -207,7 +207,7 @@ if (_.inBrowser) {
207207

208208
it('children', function () {
209209
var parent = new Vue()
210-
var child = parent.$addChild()
210+
var child = new Vue({ parent: parent })
211211
parent.$destroy()
212212
expect(child._isDestroyed).toBe(true)
213213
})

test/unit/specs/directives/public/on_spec.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -214,11 +214,12 @@ if (_.inBrowser) {
214214
test: test
215215
}
216216
})
217-
parent.$addChild({
217+
var child = new Vue({
218218
el: el,
219-
template: '<a v-on:click="$parent.test($event)"></a>'
219+
template: '<a v-on:click="$parent.test($event)"></a>',
220+
parent: parent
220221
})
221-
var e = trigger(el.firstChild, 'click')
222+
var e = trigger(child.$el.firstChild, 'click')
222223
expect(test).toHaveBeenCalledWith(e)
223224
})
224225

test/unit/specs/instance/events_spec.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,8 @@ describe('Instance Events', function () {
249249
attached: spy,
250250
detached: spy2
251251
})
252-
var childVm = parentVm.$addChild({
252+
var childVm = new Vue({
253+
parent: parentVm,
253254
el: childEl,
254255
attached: spy,
255256
detached: spy2
@@ -272,7 +273,8 @@ describe('Instance Events', function () {
272273
attached: spy,
273274
detached: spy2
274275
})
275-
var childVm = parentVm.$addChild({
276+
var childVm = new Vue({
277+
parent: parentVm,
276278
el: childEl,
277279
attached: spy,
278280
detached: spy2
@@ -294,7 +296,8 @@ describe('Instance Events', function () {
294296
el: el,
295297
attached: spy
296298
})
297-
var childVm = parentVm.$addChild({
299+
var childVm = new Vue({
300+
parent: parentVm,
298301
el: childEl,
299302
attached: spy
300303
})

test/unit/specs/transition/transition_spec.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,9 @@ if (_.inBrowser && !_.isIE9) {
107107

108108
it('skip vm with parent still being compiled', function () {
109109
el.__v_trans = new Transition(el, 'test', null, vm)
110-
var child B6A4 = vm.$addChild({
111-
el: el
110+
var child = new Vue({
111+
el: el,
112+
parent: vm
112113
})
113114
expect(child._isCompiled).toBe(true)
114115
transition.apply(el, 1, op, child, cb)

test/unit/specs/watcher_spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ describe('Watcher', function () {
129129
expect(watcher.value).toBeUndefined()
130130
expect(watcher2.value).toBeUndefined()
131131
// check $add should not affect isolated children
132-
var child2 = vm.$addChild()
132+
var child2 = new Vue({ parent: vm })
133133
var watcher3 = new Watcher(child2, 'd.e', spy)
134134
expect(watcher3.value).toBeUndefined()
135135
vm.$set('d', { e: 123 })

0 commit comments

Comments
 (0)
0