Răsfoiți Sursa

plugin interface

Evan You 12 ani în urmă
părinte
comite
a891df220f
2 a modificat fișierele cu 59 adăugiri și 0 ștergeri
  1. 25 0
      src/main.js
  2. 34 0
      test/unit/specs/api.js

+ 25 - 0
src/main.js

@@ -65,6 +65,31 @@ ViewModel.transition = function (id, transition) {
     return this
 }
 
+/**
+ *  Expose internal modules for plugins
+ */
+ViewModel.require = function (path) {
+    return require('./' + path)
+}
+
+/**
+ *  Expose an interface for plugins
+ */
+ViewModel.use = function (plugin) {
+    if (typeof plugin === 'string') {
+        try {
+            plugin = require(plugin)
+        } catch (e) {
+            return utils.warn('Cannot find plugin: ' + plugin)
+        }
+    }
+    if (typeof plugin === 'function') {
+        plugin(ViewModel)
+    } else if (plugin.install) {
+        plugin.install(ViewModel)
+    }
+}
+
 ViewModel.extend = extend
 ViewModel.nextTick = utils.nextTick
 

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

@@ -37,6 +37,40 @@ describe('UNIT: API', function () {
 
     })
 
+    describe('require()', function () {
+        
+        it('should expose internal modules', function () {
+            var c = Vue.require('config'),
+                cc = require('vue/src/config')
+            assert.strictEqual(c, cc)
+        })
+
+    })
+
+    describe('use()', function () {
+        
+        it('should install a plugin via its install function', function () {
+            var called = false
+            Vue.use({
+                install: function (vue) {
+                    called = true
+                    assert.strictEqual(vue, Vue)
+                }
+            })
+            assert.ok(called)
+        })
+
+        it('should install a plugin if its a function itself', function () {
+            var called = false
+            Vue.use(function (vue) {
+                called = true
+                assert.strictEqual(vue, Vue)
+            })
+            assert.ok(called)
+        })
+
+    })
+
     describe('filter()', function () {
 
         var reverse = function (input) {