This class tracks and handles event listeners we bind and want to unbind. This is especially useful when we need to properly
manage events when the DOM isn't completely rebuilt on a page request - as is the case with most SPAs. When navigating away
from the current route, simply call the decoupleAllEvents
method to stop listening and remove the currently monitored
events.
// Instantiate the class
let Monitor = new EventsMonitor();
// Add the event tracking
Monitor.addEvent({
el: document.querySelector("#example-anchor"),
event: 'click',
fn: event => {
console.log('anchor clicked');
}
});
// Decouple a single event
Monitor.decoupleEvent(anchor, 'click');
// Or decouple all events
Monitor.decoupleAllEvents();
Decouples listener from element and unsets event object from EventsMonitor this.events
array. Optionally pass in the index
parameter to bypass this.findEvent
and directly
unset the this.events
item at index
.
Returns true
on success. 'false' on failure.
Loops this.events
and applies a callback to each.
Returns this
Find a certain event object in this.events
. Optionally set returnIndex to true to return
the index
of the match instead of its event object.
Returns Object
|integer
on success. undefined
on failure.
Decouples all listeners currently tracked in this.events
.
Returns true
.
Loop the required event properties and apply a callback to each.
Returns this
.
Bind an event listener and push the details into this.events
. obj
parameter must contain
an object with el
, event
, and fn
properties. Like so:
let _Example = new EventsMonitor();
_Example.addEvent({
el: document.querySelector('body'),
event: 'click',
fn: () => {
console.log('clicked the body');
}
});
Returns this
.