Przeglądaj źródła

Implements plugin parameters as discussed in #88

Pierre Bertet 12 lat temu
rodzic
commit
aaf0b48fa6
3 zmienionych plików z 43 dodań i 6 usunięć
  1. 9 4
      src/main.js
  2. 31 1
      test/unit/specs/api.js
  3. 3 1
      test/unit/utils/prepare.js

+ 9 - 4
src/main.js

@@ -66,10 +66,15 @@ ViewModel.use = function (plugin) {
             return utils.warn('Cannot find plugin: ' + plugin)
         }
     }
-    if (typeof plugin === 'function') {
-        plugin(ViewModel)
-    } else if (plugin.install) {
-        plugin.install(ViewModel)
+
+    // additional parameters
+    var args = [].slice.call(arguments, 1)
+    args.unshift(ViewModel)
+
+    if (typeof plugin.install === 'function') {
+        plugin.install.apply(plugin, args)
+    } else {
+        plugin.apply(null, args)
     }
 }
 

+ 31 - 1
test/unit/specs/api.js

@@ -61,7 +61,7 @@ describe('UNIT: API', function () {
             assert.ok(called)
         })
 
-        it('should install a plugin if its a function itself', function () {
+        it('should install a plugin if its a function itself', function () {
             var called = false
             Vue.use(function (vue) {
                 called = true
@@ -70,6 +70,36 @@ describe('UNIT: API', function () {
             assert.ok(called)
         })
 
+        it('should pass any additional parameter', function () {
+            var param1 = 'a',
+                param2 = { b: 'c' }
+
+            Vue.use(function (vue, p1, p2) {
+                assert.strictEqual(p1, param1)
+                assert.strictEqual(p2, param2)
+            }, param1, param2)
+
+            Vue.use({
+                install: function (vue, p1, p2) {
+                    assert.strictEqual(p1, param1)
+                    assert.strictEqual(p2, param2)
+                }
+            }, param1, param2)
+        })
+
+        it('should properly set the value of this', function () {
+            var plugin = {
+                install: function () {
+                    assert.strictEqual(this, plugin)
+                }
+            }
+            Vue.use(plugin)
+
+            Vue.use(function () {
+                assert.strictEqual(this, global)
+            })
+        })
+
     })
 
     describe('filter()', function () {

+ 3 - 1
test/unit/utils/prepare.js

@@ -42,4 +42,6 @@ Vue.config({silent:true})
 var testDiv = document.createElement('div')
 testDiv.id = 'test'
 testDiv.style.display = 'none'
-document.body.appendChild(testDiv)
+document.body.appendChild(testDiv)
+
+var global = this