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

Add $arguments variable in component events inline statement #1945 #1949

Merged
merged 3 commits into from
Dec 2, 2015
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions src/instance/internal/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,18 @@ export default function (Vue) {

function registerComponentEvents (vm, el) {
var attrs = el.attributes
var name, handler
for (var i = 0, l = attrs.length; i < l; i++) {
name = attrs[i].name
let name = attrs[i].name
if (eventRE.test(name)) {
name = name.replace(eventRE, '')
handler = (vm._scope || vm._context).$eval(attrs[i].value, true)
vm.$on(name.replace(eventRE), handler)
const scope = vm._scope || vm._context
const handler = scope.$eval(attrs[i].value, true)
Copy link
Author

Choose a reason for hiding this comment

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

Does it ok to use ES6 syntax here? i searched whole src codes and found there is no ES6 syntax used except modules.

Copy link
Member

Choose a reason for hiding this comment

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

it's ok to use let and const. I am only using features that do not increase compiled file size at the moment.

vm.$on(name.replace(eventRE), function (...args) {
scope.$arguments = args
const result = handler.apply(this, args)
Copy link
Author

Choose a reason for hiding this comment

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

One disadvantage of doing wrapper here instead of here is, if this statement isSimplePath, it is no necessary to wrap it, so we will waste one function call here.

Copy link
Member

Choose a reason for hiding this comment

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

Good point. Let's wrap it in $get. Also, use util.toArray to convert arguments to an array, the ...args syntax adds a long for loop in the compiled code.

Copy link
Author

Choose a reason for hiding this comment

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

got it

scope.$arguments = null
return result
})
}
}
}
Expand Down
18 changes: 18 additions & 0 deletions test/unit/specs/instance/events_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,24 @@ describe('Instance Events', function () {
expect(vm.a).toBe(1)
})

it('passing $arguments', function () {
new Vue({
el: document.createElement('div'),
template: '<comp @ready="onReady($arguments[1])"></comp>',
methods: {
onReady: spy
},
components: {
comp: {
compiled: function () {
this.$emit('ready', 123, 1234)
}
}
}
})
expect(spy).toHaveBeenCalledWith(1234)
})

describe('attached/detached', function () {

it('in DOM', function () {
Expand Down