Kaynağa Gözat

support proper className for $addChild()

Evan You 11 yıl önce
ebeveyn
işleme
7d41530db6
3 değiştirilmiş dosya ile 17 ekleme ve 7 silme
  1. 6 4
      src/api/child.js
  2. 2 3
      src/api/global.js
  3. 9 0
      test/unit/specs/api/child_spec.js

+ 6 - 4
src/api/child.js

@@ -26,10 +26,12 @@ exports.$addChild = function (opts, BaseCtor) {
     }
     ChildVue = ctors[BaseCtor.cid]
     if (!ChildVue) {
-      ChildVue = function (options) {
-        this.constructor = ChildVue
-        _.Vue.call(this, options)
-      }
+      var className = BaseCtor.name || 'VueComponent'
+      ChildVue = new Function(
+        'return function ' + className + ' (options) {' +
+        'this.constructor = ' + className + ';' +
+        'this._init(options) }'
+      )()
       ChildVue.options = BaseCtor.options
       ChildVue.prototype = this
       ctors[BaseCtor.cid] = ChildVue

+ 2 - 3
src/api/global.js

@@ -55,10 +55,9 @@ exports.extend = function (extendOptions) {
 
 function createClass (name) {
   return new Function(
-    'Vue',
     'return function ' + _.camelize(name) +
-    ' (options) { Vue.call(this, options) }'
-  )(_.Vue)
+    ' (options) { this._init(options) }'
+  )()
 }
 
 /**

+ 9 - 0
test/unit/specs/api/child_spec.js

@@ -66,4 +66,13 @@ describe('Child API', function () {
     expect(child1.constructor).toBe(child2.constructor)
   })
 
+  it('Use proper constructor name with inherit', function () {
+    var Ctor = Vue.extend({
+      name: 'vue-test',
+      inherit: true
+    })
+    var child = vm.$addChild(null, Ctor)
+    expect(child.constructor.toString().match(/^function VueTest\s?\(/)).toBeTruthy()
+  })
+
 })