Sfoglia il codice sorgente

fix #487 v-ref assigned wrong owner

Evan You 11 anni fa
parent
commit
ae4584bc12

+ 3 - 2
src/directives/ref.js

@@ -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
   }
   
 }

+ 6 - 8
src/instance/init.js

@@ -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(

+ 12 - 0
test/unit/specs/directives/ref_spec.js

@@ -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,

+ 2 - 4
test/unit/specs/instance/init_spec.js

@@ -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 () {