Skip to content
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
4 changes: 4 additions & 0 deletions src/compiler/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ export function addHandler (
delete modifiers.capture
name = '!' + name // mark the event as captured
}
if (modifiers && modifiers.once) {
delete modifiers.once
name = '~' + name // mark the event as once
}
let events
if (modifiers && modifiers.native) {
delete modifiers.native
Expand Down
9 changes: 5 additions & 4 deletions src/core/vdom/helpers/update-listeners.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export function updateListeners (
remove: Function,
vm: Component
) {
let name, cur, old, fn, event, capture
let name, cur, old, fn, event, capture, once
for (name in on) {
cur = on[name]
old = oldOn[name]
Expand All @@ -20,17 +20,18 @@ export function updateListeners (
)
} else if (!old) {
capture = name.charAt(0) === '!'
event = capture ? name.slice(1) : name
once = name.charAt(0) === '~'
event = capture || once ? name.slice(1) : name
if (Array.isArray(cur)) {
add(event, (cur.invoker = arrInvoker(cur)), capture)
add(event, (cur.invoker = arrInvoker(cur)), capture, once)
} else {
if (!cur.invoker) {
fn = cur
cur = on[name] = {}
cur.fn = fn
cur.invoker = fnInvoker(cur)
}
add(event, cur.invoker, capture)
add(event, cur.invoker, capture, once)
}
} else if (cur !== old) {
if (Array.isArray(old)) {
Expand Down
16 changes: 15 additions & 1 deletion src/platforms/web/runtime/modules/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,27 @@

import { updateListeners } from 'core/vdom/helpers/index'

const onceFlags = {}
const randomHex = () => (Math.random() + '').substr(2).toString(16)

function updateDOMListeners (oldVnode, vnode) {
if (!oldVnode.data.on && !vnode.data.on) {
return
}
const on = vnode.data.on || {}
const oldOn = oldVnode.data.on || {}
const add = vnode.elm._v_add || (vnode.elm._v_add = (event, handler, capture) => {
const add = vnode.elm._v_add || (vnode.elm._v_add = (event, handler, capture, once) => {
if (once) {
const randomKey = randomHex() + new Date().valueOf().toString(16) + randomHex()
onceFlags[randomKey] = false
const oldHandler = handler
handler = () => {
if (!onceFlags[randomKey]) {
Copy link
Member

Choose a reason for hiding this comment

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

why not just remove the event handle after invoked?

Copy link
Contributor Author

@KingMario KingMario Nov 22, 2016

Choose a reason for hiding this comment

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

That's a good question.

Actually, some bugs were found and the pr is way not completed. I'm still working on it.

onceFlags[randomKey] = true
oldHandler()
}
}
}
vnode.elm.addEventListener(event, handler, capture)
})
const remove = vnode.elm._v_remove || (vnode.elm._v_remove = (event, handler) => {
Expand Down
18 changes: 18 additions & 0 deletions test/unit/features/directives/on.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,24 @@ describe('Directive v-on', () => {
expect(callOrder.toString()).toBe('1,2')
})

it('should support once', () => {
const numberPushed = []
vm = new Vue({
el,
template: `
<div @click.once="foo">
</div>
`,
methods: {
foo () { numberPushed.push(1) }
Copy link
Member

Choose a reason for hiding this comment

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

you could create a jasmine.createSpy(), and use expect(spy).not.toHaveBeenCalled() to check it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Wait for it, sir.

Copy link
Contributor Author

@KingMario KingMario Nov 22, 2016

Choose a reason for hiding this comment

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

I've made changes to this pr locally, but while running the unit test, encountered with some problem:

    triggerEvent(vm.$el, 'click')
    expect(spy).toHaveBeenCalled()
    triggerEvent(vm.$el, 'click')
    expect(spy).not.toHaveBeenCalled()

The second assertion fails.

Will the second assertion be misjudged by the first call?

  it('should support capture and once', () => {
    const callOrder = []
    vm = new Vue({
      el,
      template: `
        <div @click.capture.once="foo">
          <div @click="bar"></div>
        </div>
      `,
      methods: {
        foo () { callOrder.push(1) },
        bar () { callOrder.push(2) }
      }
    })
    triggerEvent(vm.$el.firstChild, 'click')
    expect(callOrder.toString()).toBe('1,2')
    triggerEvent(vm.$el.firstChild, 'click')
    expect(callOrder.toString()).toBe('1,2,2')
  })

Both of the assertions in this test success.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Figured out that, in this case, should use spy.calls.count().

}
})
triggerEvent(vm.$el, 'click')
expect(numberPushed.toString()).toBe('1')
triggerEvent(vm.$el, 'click')
expect(numberPushed.toString()).toBe('1')
})

it('should support keyCode', () => {
vm = new Vue({
el,
Expand Down
7 changes: 7 additions & 0 deletions test/unit/modules/compiler/codegen.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,13 @@ describe('codegen', () => {
)
})

it('generate events with once modifier', () => {
assertCodegen(
'<input @input.once="onInput">',
`with(this){return _h('input',{on:{"~input":function($event){onInput($event)}}})}`
)
})

it('generate events with inline statement', () => {
assertCodegen(
'<input @input="curent++">',
Expand Down