Skip to content

Commit 0893163

Browse files
committed
add parent option
1 parent e50c4e7 commit 0893163

File tree

8 files changed

+88
-56
lines changed

8 files changed

+88
-56
lines changed

src/compiler.js

+15-12
Original file line numberDiff line numberDiff line change
@@ -64,21 +64,24 @@ function Compiler (vm, options) {
6464
def(vm, '$', makeHash())
6565
def(vm, '$el', el)
6666
def(vm, '$compiler', compiler)
67-
def(vm, '$root', getRoot(compiler).vm)
6867

6968
// set parent VM
7069
// and register child id on parent
71-
var parent = compiler.parentCompiler,
70+
var parentVM = options.parent,
7271
childId = utils.attr(el, 'ref')
73-
if (parent) {
74-
parent.childCompilers.push(compiler)
75-
def(vm, '$parent', parent.vm)
72+
if (parentVM) {
73+
compiler.parent = parentVM.$compiler
74+
parentVM.$compiler.childCompilers.push(compiler)
75+
def(vm, '$parent', parentVM)
7676
if (childId) {
7777
compiler.childId = childId
78-
parent.vm.$[childId] = vm
78+
parentVM.$[childId] = vm
7979
}
8080
}
8181

82+
// set root
83+
def(vm, '$root', getRoot(compiler).vm)
84+
8285
// setup observer
8386
compiler.setupObserver()
8487

@@ -546,7 +549,7 @@ CompilerProto.bindDirective = function (directive) {
546549
if (compiler.hasKey(key)) {
547550
break
548551
} else {
549-
compiler = compiler.parentCompiler
552+
compiler = compiler.parent
550553
}
551554
}
552555
compiler = compiler || this
@@ -717,7 +720,7 @@ CompilerProto.markComputed = function (binding, value) {
717720
*/
718721
CompilerProto.getOption = function (type, id) {
719722
var opts = this.options,
720-
parent = this.parentCompiler,
723+
parent = this.parent,
721724
globalAssets = config.globalAssets
722725
return (opts[type] && opts[type][id]) || (
723726
parent
@@ -844,8 +847,8 @@ CompilerProto.destroy = function () {
844847
el.removeEventListener(key, delegators[key].handler)
845848
}
846849

847-
// remove self from parentCompiler
848-
var parent = compiler.parentCompiler,
850+
// remove self from parent
851+
var parent = compiler.parent,
849852
childId = compiler.childId
850853
if (parent) {
851854
parent.childCompilers.splice(parent.childCompilers.indexOf(compiler), 1)
@@ -877,8 +880,8 @@ CompilerProto.destroy = function () {
877880
* shorthand for getting root compiler
878881
*/
879882
function getRoot (compiler) {
880-
while (compiler.parentCompiler) {
881-
compiler = compiler.parentCompiler
883+
while (compiler.parent) {
884+
compiler = compiler.parent
882885
}
883886
return compiler
884887
}

src/directives/repeat.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -201,10 +201,10 @@ module.exports = {
201201
*/
202202
dryBuild: function () {
203203
new this.Ctor({
204-
el: this.el.cloneNode(true),
204+
el : this.el.cloneNode(true),
205+
parent : this.vm,
205206
compilerOptions: {
206-
repeat: true,
207-
parentCompiler: this.compiler
207+
repeat: true
208208
}
209209
}).$destroy()
210210
this.initiated = true
@@ -265,11 +265,11 @@ module.exports = {
265265
data.$index = index
266266
// initialize the new VM
267267
item = new this.Ctor({
268-
el: el,
269-
data: data,
268+
el : el,
269+
data : data,
270+
parent : this.vm,
270271
compilerOptions: {
271-
repeat: true,
272-
parentCompiler: this.compiler
272+
repeat: true
273273
}
274274
})
275275
// for non-object values, listen for value change

src/directives/with.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,14 @@ module.exports = {
4747
data[this.arg] = value
4848
}
4949
this.subVM = new Ctor({
50-
el: this.el,
51-
data: data,
50+
el : this.el,
51+
data : data,
52+
parent : this.vm,
5253
compilerOptions: {
5354
// it is important to delay the ready hook
5455
// so that when it's called, all `v-with` wathcers
5556
// would have been set up.
56-
delayReady: !this.last,
57-
parentCompiler: this.compiler
57+
delayReady: !this.last
5858
}
5959
})
6060
},

src/exp-parser.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ function getRel (path, compiler) {
6161
if (compiler.hasKey(path)) {
6262
break
6363
} else {
64-
compiler = compiler.parentCompiler
64+
compiler = compiler.parent
6565
dist++
6666
}
6767
}

src/viewmodel.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ def(VMProto, '$broadcast', function () {
9999
def(VMProto, '$dispatch', function () {
100100
var compiler = this.$compiler,
101101
emitter = compiler.emitter,
102-
parent = compiler.parentCompiler
102+
parent = compiler.parent
103103
emitter.emit.apply(emitter, arguments)
104104
if (parent) {
105105
parent.vm.$dispatch.apply(parent.vm, arguments)
+21-27
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<style type="text/css">
2-
div:not(#grandpa) {
2+
man, offspring {
3+
display: block;
34
padding-left: 15px;
45
}
56
p {
@@ -14,61 +15,54 @@
1415
}
1516
</style>
1617

17-
<div id="grandpa" data-name="Andy" data-family="Johnson">
18+
<meta charset="utf8">
19+
<div id="grandpa" name="Andy">
1820
<p class="ancestor">{{name}} {{family}}</p>
1921

20-
<div v-component="man" data-name="Jack">
22+
<man name="Jack">
2123
<p class="jack">{{name}}, son of {{$parent.name}}</p>
2224

23-
<div v-component="man" data-name="Mike">
25+
<man name="Mike">
2426
<p class="mike">{{name}}, son of {{$parent.name}}</p>
2527

26-
<div v-component="offspring" data-name="Tim" class="tim">
27-
</div>
28+
<offspring name="Tim" class="tim"></offspring>
2829

29-
<div v-component="offspring" data-name="Tom" class="tom">
30-
</div>
31-
</div>
30+
<offspring name="Tom" class="tom"></offspring>
31+
</man>
3232

33-
<div v-component="man" data-name="Jason">
33+
<man name="Jason">
3434
<p class="jason">{{name}}, son of {{$parent.name}}</p>
3535

36-
<div v-component="offspring" data-name="Andrew" class="andrew">
37-
</div>
38-
</div>
39-
</div>
36+
<offspring name="Andrew" class="andrew"></offspring>
37+
</man>
38+
</man>
4039
</div>
4140

42-
<script type="text/v-template" id="v-template-offspring">
41+
<script type="text/x-template" id="offspring-template">
4342
<p>{{name}}, son of {{$parent.name}}, grandson of {{$parent.$parent.name}}, great-grandson of {{$root.name}}, and offspring of family {{family}}.</p>
4443
</script>
4544

4645
<script src="../../../dist/vue.js"></script>
4746
<script>
48-
Vue.config({ debug: true })
47+
//Vue.config({ debug: true })
4948

5049
var Man = Vue.extend({
51-
created: function () {
52-
this.name = this.$el.dataset.name
53-
console.log(this.$el)
54-
console.log(this.name)
55-
var family = this.$el.dataset.family
56-
if (family) {
57-
this.family = family
58-
}
59-
}
50+
paramAttributes: ['name']
6051
})
6152

6253
var Offspring = Man.extend({
63-
template: document.getElementById('v-template-offspring').innerHTML.trim()
54+
template: '#offspring-template'
6455
})
6556

6657
Vue
6758
.component('man', Man)
6859
.component('offspring', Offspring)
6960

7061
new Man({
71-
el: '#grandpa'
62+
el: '#grandpa',
63+
data: {
64+
family: 'Johnson'
65+
}
7266
})
7367

7468
</script>

test/unit/specs/api.js

+35
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,41 @@ describe('UNIT: API', function () {
634634

635635
})
636636

637+
describe('parent', function () {
638+
639+
it('should create parent-child relation between VMs', function () {
640+
641+
var parent = new Vue({
642+
data: {
643+
test: 'from parent'
644+
}
645+
})
646+
647+
var child = new Vue({
648+
parent: parent,
649+
template: '{{test}}'
650+
})
651+
652+
assert.strictEqual(child.$el.textContent, 'from parent')
653+
654+
var dispatched = false,
655+
broadcasted = false
656+
parent.$on('dispatch', function () {
657+
dispatched = true
658+
})
659+
child.$on('broadcast', function () {
660+
broadcasted = true
661+
})
662+
parent.$broadcast('broadcast')
663+
child.$dispatch('dispatch')
664+
665+
assert.ok(dispatched)
666+
assert.ok(broadcasted)
667+
668+
})
669+
670+
})
671+
637672
describe('directives', function () {
638673

639674
it('should allow the VM to use private directives', function (done) {

test/unit/specs/viewmodel.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ describe('UNIT: ViewModel', function () {
457457
}],
458458
bindings: bindingsMock,
459459
childId: 'test',
460-
parentCompiler: {
460+
parent: {
461461
childCompilers: [],
462462
vm: {
463463
$: {
@@ -487,7 +487,7 @@ describe('UNIT: ViewModel', function () {
487487
}
488488
}
489489

490-
compilerMock.parentCompiler.childCompilers.push(compilerMock)
490+
compilerMock.parent.childCompilers.push(compilerMock)
491491

492492
destroy.call(compilerMock)
493493

@@ -521,8 +521,8 @@ describe('UNIT: ViewModel', function () {
521521
assert.ok(bindingUnbindCalled)
522522
})
523523

524-
it('should remove self from parentCompiler', function () {
525-
var parent = compilerMock.parentCompiler
524+
it('should remove self from parent', function () {
525+
var parent = compilerMock.parent
526526
assert.ok(parent.childCompilers.indexOf(compilerMock), -1)
527527
assert.strictEqual(parent.vm.$[compilerMock.childId], undefined)
528528
})

0 commit comments

Comments
 (0)