Skip to content

Commit d7e40b5

Browse files
committed
improve component constructor console output
1 parent 7c5492a commit d7e40b5

File tree

5 files changed

+40
-4
lines changed

5 files changed

+40
-4
lines changed

src/api/global.js

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,15 @@ var cid = 1
2525
*/
2626

2727
exports.extend = function (extendOptions) {
28+
extendOptions = extendOptions || {}
2829
var Super = this
29-
var Sub = function (instanceOptions) {
30-
_.Vue.call(this, instanceOptions)
31-
}
30+
var Sub = createClass(extendOptions.id || 'VueComponent')
3231
Sub.prototype = Object.create(Super.prototype)
3332
Sub.prototype.constructor = Sub
3433
Sub.cid = cid++
3534
Sub.options = mergeOptions(
3635
Super.options,
37-
extendOptions || {}
36+
extendOptions
3837
)
3938
Sub['super'] = Super
4039
// allow further extension
@@ -45,6 +44,23 @@ exports.extend = function (extendOptions) {
4544
return Sub
4645
}
4746

47+
/**
48+
* A function that returns a sub-class constructor with the
49+
* given name. This gives us much nicer output when
50+
* logging instances in the console.
51+
*
52+
* @param {String} name
53+
* @return {Function}
54+
*/
55+
56+
function createClass (name) {
57+
return new Function(
58+
'Vue',
59+
'return function ' + _.camelize(name) +
60+
' (options) { Vue.call(this, options) }'
61+
)(_.Vue)
62+
}
63+
4864
/**
4965
* Plugin system
5066
*
@@ -107,6 +123,7 @@ function createAssetRegisters (Constructor) {
107123
return this.options.components[id]
108124
} else {
109125
if (_.isPlainObject(definition)) {
126+
definition.id = id
110127
definition = _.Vue.extend(definition)
111128
}
112129
this.options.components[id] = definition

src/util/lang.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,20 @@ exports.stripQuotes = function (str) {
5656
: false
5757
}
5858

59+
/**
60+
* Camelize a hyphen-delmited string.
61+
*
62+
* @param {String} str
63+
* @return {String}
64+
*/
65+
66+
var camelRE = /(?:^|[-_])(\w)/g
67+
exports.camelize = function (str) {
68+
return str.replace (camelRE, function (_, c) {
69+
return c ? c.toUpperCase () : '';
70+
})
71+
}
72+
5973
/**
6074
* Simple bind, faster than native
6175
*

src/util/merge-option.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ function guardComponents (components) {
183183
for (var key in components) {
184184
def = components[key]
185185
if (_.isPlainObject(def)) {
186+
def.id = key
186187
components[key] = _.Vue.extend(def)
187188
}
188189
}

test/unit/specs/api/global_spec.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@ describe('Global API', function () {
1212

1313
it('extend', function () {
1414
var Test = Vue.extend({
15+
id: 'test',
1516
a: 1,
1617
b: 2
1718
})
1819
expect(Test.options.a).toBe(1)
1920
expect(Test.options.b).toBe(2)
2021
expect(Test.super).toBe(Vue)
22+
expect(Test.name).toBe('Test')
2123
var t = new Test({
2224
a: 2
2325
})
@@ -81,6 +83,7 @@ describe('Global API', function () {
8183
expect(typeof component).toBe('function')
8284
expect(component.super).toBe(Vue)
8385
expect(component.options.a).toBe(1)
86+
expect(component.options.id).toBe('test')
8487
expect(Test.component('test')).toBe(component)
8588
// already extended
8689
Test.component('test2', component)

test/unit/specs/util/merge-option_spec.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ describe('Util - Option merging', function () {
141141
}
142142
})
143143
expect(typeof res.components.a).toBe('function')
144+
expect(res.components.a.options.id).toBe('a')
144145
expect(res.components.a.super).toBe(Vue)
145146
})
146147

0 commit comments

Comments
 (0)