Skip to content

Commit bf9edef

Browse files
committed
Tweak parent directive compilation
- Make `this.el.__vue__` avaialable in a parent directive on the component container. - Move `v-ref` compilation to parent instead of child
1 parent cdd1e09 commit bf9edef

File tree

4 files changed

+17
-10
lines changed

4 files changed

+17
-10
lines changed

src/compiler/compile.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,6 @@ function collectDirectives (el, options, asParent) {
472472
dirName = attrName.slice(config.prefix.length)
473473
if (asParent &&
474474
(dirName === 'with' ||
475-
dirName === 'ref' ||
476475
dirName === 'component')) {
477476
continue
478477
}

src/directives/ref.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,26 @@ module.exports = {
55
isLiteral: true,
66

77
bind: function () {
8-
if (this.el !== this.vm.$el) {
8+
var child = this.el.__vue__
9+
if (!child) {
910
_.warn(
1011
'v-ref should only be used on instance root nodes.'
1112
)
1213
return
1314
}
14-
this.owner = this.vm.$parent
15-
this.owner.$[this.expression] = this.vm
15+
if (this.vm !== child.$parent) {
16+
_.warn(
17+
'v-ref should be used from the parent template,' +
18+
' not the component\'s.'
19+
)
20+
return
21+
}
22+
this.vm.$[this.expression] = child
1623
},
1724

1825
unbind: function () {
19-
if (this.owner.$[this.expression] === this.vm) {
20-
delete this.owner.$[this.expression]
26+
if (this.vm.$[this.expression] === this.el.__vue__) {
27+
delete this.vm.$[this.expression]
2128
}
2229
}
2330

src/instance/compile.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,14 @@ exports._compile = function (el) {
4646
}
4747
// tranclude, this possibly replaces original
4848
el = transclude(el, options)
49+
this._initElement(el)
4950
// now call the container linker on the resolved el
5051
this._containerUnlinkFn = containerLinkFn(parent, el)
5152
} else {
5253
// simply transclude
5354
el = transclude(el, options)
55+
this._initElement(el)
5456
}
55-
this._initElement(el)
5657
var linkFn = compile(el, options)
5758
linkFn(this, el)
5859
if (options.replace) {

test/unit/specs/compiler/compile_spec.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,13 +204,13 @@ if (_.inBrowser) {
204204
expect(vm._bindDir.calls.count()).toBe(0)
205205
})
206206

207-
it('component parent scope compilation should skip v-ref, v-with & v-component', function () {
208-
el.innerHTML = '<div v-component v-ref="test" v-with="test"></div>'
207+
it('component parent scope compilation should skip v-with & v-component', function () {
208+
el.innerHTML = '<div v-component v-with="test"></div>'
209209
el = el.firstChild
210210
var linker = compile(el, Vue.options, true, true)
211211
linker(vm, el)
212212
expect(vm._directives.length).toBe(0)
213-
expect(el.attributes.length).toBe(3)
213+
expect(el.attributes.length).toBe(2)
214214
})
215215

216216
})

0 commit comments

Comments
 (0)