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
This commit is contained in:
Evan You 2014-12-06 18:47:27 -05:00
parent cdd1e095b5
commit bf9edefca0
4 changed files with 17 additions and 10 deletions

View File

@ -472,7 +472,6 @@ function collectDirectives (el, options, asParent) {
dirName = attrName.slice(config.prefix.length)
if (asParent &&
(dirName === 'with' ||
dirName === 'ref' ||
dirName === 'component')) {
continue
}

View File

@ -5,19 +5,26 @@ module.exports = {
isLiteral: true,
bind: function () {
if (this.el !== this.vm.$el) {
var child = this.el.__vue__
if (!child) {
_.warn(
'v-ref should only be used on instance root nodes.'
)
return
}
this.owner = this.vm.$parent
this.owner.$[this.expression] = this.vm
if (this.vm !== child.$parent) {
_.warn(
'v-ref should be used from the parent template,' +
' not the component\'s.'
)
return
}
this.vm.$[this.expression] = child
},
unbind: function () {
if (this.owner.$[this.expression] === this.vm) {
delete this.owner.$[this.expression]
if (this.vm.$[this.expression] === this.el.__vue__) {
delete this.vm.$[this.expression]
}
}

View File

@ -46,13 +46,14 @@ exports._compile = function (el) {
}
// tranclude, this possibly replaces original
el = transclude(el, options)
this._initElement(el)
// now call the container linker on the resolved el
this._containerUnlinkFn = containerLinkFn(parent, el)
} else {
// simply transclude
el = transclude(el, options)
this._initElement(el)
}
this._initElement(el)
var linkFn = compile(el, options)
linkFn(this, el)
if (options.replace) {

View File

@ -204,13 +204,13 @@ if (_.inBrowser) {
expect(vm._bindDir.calls.count()).toBe(0)
})
it('component parent scope compilation should skip v-ref, v-with & v-component', function () {
el.innerHTML = '<div v-component v-ref="test" v-with="test"></div>'
it('component parent scope compilation should skip v-with & v-component', function () {
el.innerHTML = '<div v-component v-with="test"></div>'
el = el.firstChild
var linker = compile(el, Vue.options, true, true)
linker(vm, el)
expect(vm._directives.length).toBe(0)
expect(el.attributes.length).toBe(3)
expect(el.attributes.length).toBe(2)
})
})