Browse Source

allow components to use plugins too, resolve Browserify Vue.require issue

Evan You 12 years ago
parent
commit
74c03f3e12
2 changed files with 28 additions and 10 deletions
  1. 20 10
      src/main.js
  2. 8 0
      test/unit/specs/api.js

+ 20 - 10
src/main.js

@@ -4,6 +4,11 @@ var config      = require('./config'),
     makeHash    = utils.hash,
     makeHash    = utils.hash,
     assetTypes  = ['directive', 'filter', 'partial', 'transition', 'component']
     assetTypes  = ['directive', 'filter', 'partial', 'transition', 'component']
 
 
+// require these so Browserify can catch them
+// so they can be used in Vue.require
+require('./observer')
+require('./transition')
+
 ViewModel.options = config.globalAssets = {
 ViewModel.options = config.globalAssets = {
     directives  : require('./directives'),
     directives  : require('./directives'),
     filters     : require('./filters'),
     filters     : require('./filters'),
@@ -48,13 +53,6 @@ ViewModel.config = function (opts, val) {
     return this
     return this
 }
 }
 
 
-/**
- *  Expose internal modules for plugins
- */
-ViewModel.require = function (path) {
-    return require('./' + path)
-}
-
 /**
 /**
  *  Expose an interface for plugins
  *  Expose an interface for plugins
  */
  */
@@ -69,13 +67,21 @@ ViewModel.use = function (plugin) {
 
 
     // additional parameters
     // additional parameters
     var args = [].slice.call(arguments, 1)
     var args = [].slice.call(arguments, 1)
-    args.unshift(ViewModel)
+    args.unshift(this)
 
 
     if (typeof plugin.install === 'function') {
     if (typeof plugin.install === 'function') {
         plugin.install.apply(plugin, args)
         plugin.install.apply(plugin, args)
     } else {
     } else {
         plugin.apply(null, args)
         plugin.apply(null, args)
     }
     }
+    return this
+}
+
+/**
+ *  Expose internal modules for plugins
+ */
+ViewModel.require = function (path) {
+    return require('./' + path)
 }
 }
 
 
 ViewModel.extend = extend
 ViewModel.extend = extend
@@ -118,8 +124,8 @@ function extend (options) {
     }
     }
 
 
     // allow extended VM to be further extended
     // allow extended VM to be further extended
-    ExtendedVM.extend = extend
-    ExtendedVM.super = ParentVM
+    ExtendedVM.extend  = extend
+    ExtendedVM.super   = ParentVM
     ExtendedVM.options = options
     ExtendedVM.options = options
 
 
     // allow extended VM to add its own assets
     // allow extended VM to add its own assets
@@ -127,6 +133,10 @@ function extend (options) {
         ExtendedVM[type] = ViewModel[type]
         ExtendedVM[type] = ViewModel[type]
     })
     })
 
 
+    // allow extended VM to use plugins
+    ExtendedVM.use     = ViewModel.use
+    ExtendedVM.require = ViewModel.require
+
     return ExtendedVM
     return ExtendedVM
 }
 }
 
 

+ 8 - 0
test/unit/specs/api.js

@@ -380,6 +380,14 @@ describe('UNIT: API', function () {
             assert.ok(Sub.options.partials.test instanceof window.DocumentFragment)
             assert.ok(Sub.options.partials.test instanceof window.DocumentFragment)
         })
         })
 
 
+        it('should allow subclasses to use plugins', function () {
+            var Sub = Vue.extend({})
+            Sub.use(function (Sub) {
+                Sub.directive('hello', {})
+            })
+            assert.ok(Sub.options.directives.hello)
+        })
+
         describe('Options', function () {
         describe('Options', function () {
 
 
             describe('methods', function () {
             describe('methods', function () {