Skip to content

Commit 74c03f3

Browse files
author
Evan You
committed
allow components to use plugins too, resolve Browserify Vue.require issue
1 parent ff3b608 commit 74c03f3

File tree

2 files changed

+28
-10
lines changed

2 files changed

+28
-10
lines changed

src/main.js

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ var config = require('./config'),
44
makeHash = utils.hash,
55
assetTypes = ['directive', 'filter', 'partial', 'transition', 'component']
66

7+
// require these so Browserify can catch them
8+
// so they can be used in Vue.require
9+
require('./observer')
10+
require('./transition')
11+
712
ViewModel.options = config.globalAssets = {
813
directives : require('./directives'),
914
filters : require('./filters'),
@@ -48,13 +53,6 @@ ViewModel.config = function (opts, val) {
4853
return this
4954
}
5055

51-
/**
52-
* Expose internal modules for plugins
53-
*/
54-
ViewModel.require = function (path) {
55-
return require('./' + path)
56-
}
57-
5856
/**
5957
* Expose an interface for plugins
6058
*/
@@ -69,13 +67,21 @@ ViewModel.use = function (plugin) {
6967

7068
// additional parameters
7169
var args = [].slice.call(arguments, 1)
72-
args.unshift(ViewModel)
70+
args.unshift(this)
7371

7472
if (typeof plugin.install === 'function') {
7573
plugin.install.apply(plugin, args)
7674
} else {
7775
plugin.apply(null, args)
7876
}
77+
return this
78+
}
79+
80+
/**
81+
* Expose internal modules for plugins
82+
*/
83+
ViewModel.require = function (path) {
84+
return require('./' + path)
7985
}
8086

8187
ViewModel.extend = extend
@@ -118,15 +124,19 @@ function extend (options) {
118124
}
119125

120126
// allow extended VM to be further extended
121-
ExtendedVM.extend = extend
122-
ExtendedVM.super = ParentVM
127+
ExtendedVM.extend = extend
128+
ExtendedVM.super = ParentVM
123129
ExtendedVM.options = options
124130

125131
// allow extended VM to add its own assets
126132
assetTypes.forEach(function (type) {
127133
ExtendedVM[type] = ViewModel[type]
128134
})
129135

136+
// allow extended VM to use plugins
137+
ExtendedVM.use = ViewModel.use
138+
ExtendedVM.require = ViewModel.require
139+
130140
return ExtendedVM
131141
}
132142

test/unit/specs/api.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,14 @@ describe('UNIT: API', function () {
380380
assert.ok(Sub.options.partials.test instanceof window.DocumentFragment)
381381
})
382382

383+
it('should allow subclasses to use plugins', function () {
384+
var Sub = Vue.extend({})
385+
Sub.use(function (Sub) {
386+
Sub.directive('hello', {})
387+
})
388+
assert.ok(Sub.options.directives.hello)
389+
})
390+
383391
describe('Options', function () {
384392

385393
describe('methods', function () {

0 commit comments

Comments
 (0)