Subscribe-event
Subscribe-event is the easiest way to subscribe either dom events in browser or node.js events.
Live example: http://xnimorz.github.io/subscribe-event/
Install
npm install --save subscribe-event
// or for yarn:
yarn add subscribe-event
Library support es6, CommonJs, AMD modules
Getting started
Step 1: import module:
;
Step 2: call subscrube function:
const unsubscribe = ;
Step 3: call unsubscibe, which returned from subscribe function, to dispose:
unsubscribe();
API
subscribe
is a function, which receives 4 arguments:
- element — is a HTMLElement such as document.body, document, etc., Node.js object or any another object, that triggers events.
- event — is an event, such as
scroll
,click
,mousemove
etc. - eventCallback — a function called when event triggers
- options — unessesary parameter, which provides more data to subscribe function. For example, when you using
subscribe
for HTMLElements you can capture events:subscibe(document.querySelector('#a'), 'click', () => console.log('clicked'), true)
Usage with React
;; { thisunsubscribeScroll = ; } { this; } { const children = thisprops; return <div>children</div>; }
Subscribe-event is useful for capturing events:
;; { thisunsubscribe = ; } { this; } { const children = thisprops; return <div>children</div>; }
Examples
For example in browser:
; var element = document; var unsubscribe = ; // code ;
or
; var unsubscribe = ; // code ;
This library supports old ie also (attachEvent/detachEvent).
You can define your custom event subscribe function:
; var obj: {...} {...} var customSubscribe = subscribe; var customUnsubscribe = ; // ... ;
By default subscribe-event works with: addEventListener
, detachEvent
, on