npm install -D @8pattern/jevent
- import the module
import jEvent from '@8pattern/jevent'
or
const jEvent = require('@8pattern/jevent')
- bind a certain object
const tareget = document.body
const handle = () => {}
// suscribe
// the return value equals handle
jEvent(target).on('eventName', handle)
jEvent(target).on(['event1', 'event2'], handle)
// publish
jEvent(target).emit('eventName', ['arg1', 'arg2'])
jEvent(target).emit(['event1', 'event2'])
// off
// the return value indicates whether the handle was removed
jEvent(target).off('eventName', handle)
// clear
// the return value indicates whether the event was removed
jEvent(target).clear('eventName')
if no object binding, jEvent will binding a global object by default. In other words, the following statements also work.
const handle = jEvent().on('eventName', () => {})
jEvent().emit('eventName')
jEvent().off('eventName', handle)
jEvent().clear('eventName')
or
const handle = jEvent.on('eventName', () => {})
jEvent.emit('eventName')
jEvent.off('eventName', handle)
jEvent.clear('eventName')