fix #487 v-ref assigned wrong owner

This commit is contained in:
Evan You 2014-10-14 13:04:05 -04:00
parent 3c487aea47
commit ae4584bc12
4 changed files with 23 additions and 14 deletions

View File

@ -11,11 +11,12 @@ module.exports = {
)
return
}
this.vm._owner.$[this.expression] = this.vm
this.owner = this.vm.$parent._owner
this.owner.$[this.expression] = this.vm
},
unbind: function () {
this.vm._owner.$[this.expression] = null
this.owner.$[this.expression] = null
}
}

View File

@ -51,14 +51,12 @@ exports._init = function (options) {
// child constructors
// anonymous instances are created by v-if
// we need to walk along the parent chain to locate the
// first non-anonymous instance as the owner.
this._isAnonymous = options._anonymous
var parent = this.$parent
while (parent && parent._isAnonymous) {
parent = parent.$parent
}
this._owner = parent || this
// if an instance is anonymous, its owner will be the
// first non-anonymous parent; otherwise its owner will
// be itself.
this._owner = options._anonymous
? this.$parent._owner
: this
// merge options.
options = this.$options = mergeOptions(

View File

@ -60,6 +60,18 @@ if (_.inBrowser) {
expect(vm.$.test).toBeNull()
})
it('nested v-repeat', function () {
var vm = new Vue({
el: el,
template: '<div v-component="c1" v-ref="c1"><div v-repeat="2" v-ref="c2"></div></div>',
components: { c1: {} }
})
expect(vm.$.c1 instanceof Vue).toBe(true)
expect(vm.$.c2).toBeUndefined()
expect(Array.isArray(vm.$.c1.$.c2)).toBe(true)
expect(vm.$.c1.$.c2.length).toBe(2)
})
it('warn on non-root', function () {
var vm = new Vue({
el: el,

View File

@ -16,8 +16,7 @@ describe('Instance Init', function () {
a: 2,
_anonymous: true,
_parent: {
_isAnonymous: true,
$parent: {}
_owner: {}
},
el: {}
}
@ -34,7 +33,6 @@ describe('Instance Init', function () {
expect(stub._directives).toBeTruthy()
expect(stub._events).toBeTruthy()
expect(stub._eventsCount).toBeTruthy()
expect(stub._isAnonymous).toBe(true)
})
it('should merge options', function () {
@ -43,7 +41,7 @@ describe('Instance Init', function () {
})
it('should locate owner', function () {
expect(stub._owner).toBe(options._parent.$parent)
expect(stub._owner).toBe(options._parent._owner)
})
it('should call other init methods', function () {