Skip to content

Commit ab8b0e9

Browse files
committed
simplify asset registration method creation
1 parent a901c06 commit ab8b0e9

File tree

2 files changed

+17
-36
lines changed

2 files changed

+17
-36
lines changed

src/api/global.js

Lines changed: 16 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ exports.extend = function (extendOptions) {
5353
Sub.extend = Super.extend
5454
// create asset registers, so extended classes
5555
// can have their private assets too.
56-
createAssetRegisters(Sub)
56+
config._assetTypes.forEach(function (type) {
57+
Sub[type] = Super[type]
58+
})
5759
return Sub
5860
}
5961

@@ -92,48 +94,26 @@ exports.use = function (plugin) {
9294
}
9395

9496
/**
95-
* Define asset registration methods on a constructor.
97+
* Create asset registration methods with the following
98+
* signature:
9699
*
97-
* @param {Function} Constructor
100+
* @param {String} id
101+
* @param {*} definition
98102
*/
99103

100-
function createAssetRegisters (Constructor) {
101-
102-
/* Asset registration methods share the same signature:
103-
*
104-
* @param {String} id
105-
* @param {*} definition
106-
*/
107-
108-
config._assetTypes.forEach(function (type) {
109-
Constructor[type] = function (id, definition) {
110-
if (!definition) {
111-
return this.options[type + 's'][id]
112-
} else {
113-
this.options[type + 's'][id] = definition
114-
}
115-
}
116-
})
117-
118-
/**
119-
* Component registration needs to automatically invoke
120-
* Vue.extend on object values.
121-
*
122-
* @param {String} id
123-
* @param {Object|Function} definition
124-
*/
125-
126-
Constructor.component = function (id, definition) {
104+
config._assetTypes.forEach(function (type) {
105+
exports[type] = function (id, definition) {
127106
if (!definition) {
128-
return this.options.components[id]
107+
return this.options[type + 's'][id]
129108
} else {
130-
if (_.isPlainObject(definition)) {
109+
if (
110+
type === 'component' &&
111+
_.isPlainObject(definition)
112+
) {
131113
definition.name = id
132114
definition = _.Vue.extend(definition)
133115
}
134-
this.options.components[id] = definition
116+
this.options[type + 's'][id] = definition
135117
}
136118
}
137-
}
138-
139-
createAssetRegisters(exports)
119+
})

src/config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ module.exports = {
7171
*/
7272

7373
_assetTypes: [
74+
'component',
7475
'directive',
7576
'elementDirective',
7677
'filter',

0 commit comments

Comments
 (0)