Skip to content

Commit 12430aa

Browse files
committed
Added the ability to decorate without options
1 parent d188c7f commit 12430aa

File tree

1 file changed

+40
-30
lines changed

1 file changed

+40
-30
lines changed

index.js

Lines changed: 40 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -15,38 +15,48 @@ var internalHooks = [
1515
'activate'
1616
]
1717

18-
function decorator (options) {
19-
return function (Component) {
20-
options.name = options.name || Component.name
21-
// prototype props.
22-
var proto = Component.prototype
23-
Object.getOwnPropertyNames(proto).forEach(function (key) {
24-
if (key === 'constructor') {
25-
return
26-
}
27-
// hooks
28-
if (internalHooks.indexOf(key) > -1) {
29-
options[key] = proto[key]
30-
return
31-
}
32-
var descriptor = Object.getOwnPropertyDescriptor(proto, key)
33-
if (typeof descriptor.value === 'function') {
34-
// methods
35-
(options.methods || (options.methods = {}))[key] = descriptor.value
36-
} else if (descriptor.get || descriptor.set) {
37-
// computed properties
38-
(options.computed || (options.computed = {}))[key] = {
39-
get: descriptor.get,
40-
set: descriptor.set
41-
}
18+
function componentFactory (Component, options) {
19+
if (!options) {
20+
options = {}
21+
}
22+
options.name = options.name || Component.name
23+
// prototype props.
24+
var proto = Component.prototype
25+
Object.getOwnPropertyNames(proto).forEach(function (key) {
26+
if (key === 'constructor') {
27+
return
28+
}
29+
// hooks
30+
if (internalHooks.indexOf(key) > -1) {
31+
options[key] = proto[key]
32+
return
33+
}
34+
var descriptor = Object.getOwnPropertyDescriptor(proto, key)
35+
if (typeof descriptor.value === 'function') {
36+
// methods
37+
(options.methods || (options.methods = {}))[key] = descriptor.value
38+
} else if (descriptor.get || descriptor.set) {
39+
// computed properties
40+
(options.computed || (options.computed = {}))[key] = {
41+
get: descriptor.get,
42+
set: descriptor.set
4243
}
43-
})
44-
// find super
45-
var Super = proto.__proto__.constructor
46-
if (!(Super instanceof Vue)) {
47-
Super = Vue
4844
}
49-
return Super.extend(options)
45+
})
46+
// find super
47+
var Super = proto.__proto__.constructor
48+
if (!(Super instanceof Vue)) {
49+
Super = Vue
50+
}
51+
return Super.extend(options)
52+
}
53+
54+
function decorator (options) {
55+
if (typeof options === 'function') {
56+
return componentFactory(options)
57+
}
58+
return function (Component) {
59+
return componentFactory(Component, options)
5060
}
5161
}
5262

0 commit comments

Comments
 (0)