Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implements plugin parameters #118

Merged
merged 1 commit into from
Feb 20, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 9 additions & 4 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don’t do any check here, I think it’s fine to crash if a plugin author tries to .use() a plugin with a wrong interface.

}
}

Expand Down
32 changes: 31 additions & 1 deletion test/unit/specs/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 it’s a function itself', function () {
var called = false
Vue.use(function (vue) {
called = true
Expand All @@ -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 () {
Expand Down
4 changes: 3 additions & 1 deletion test/unit/utils/prepare.js
Original file line number Diff line number Diff line change
Expand Up @@ -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