Skip to content

A non-dependent library to achieve subscribe/publish partten for JS

License

Notifications You must be signed in to change notification settings

8pattern/jEvent

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Installation

npm install -D @8pattern/jevent

Usage

  1. import the module
import jEvent from '@8pattern/jevent'

​ or

const jEvent = require('@8pattern/jevent')
  1. 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')

About

A non-dependent library to achieve subscribe/publish partten for JS

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published