-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremove-listeners-behavior.js
35 lines (32 loc) · 1.3 KB
/
remove-listeners-behavior.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/* global window */
(() => {
window.TSPolymerBehaviors = window.TSPolymerBehaviors || {};
/**
* `TSPolymerBehaviors.RemoveListenersBehavior` allows an element to remove all of its listeners automatically when the `detached` callback is executed.
*
* Polymer components that consume this behavior have to use their own `registerEventListener` method to add new listeners in order for them to be removed by the behavior's `detached` callback.
*
* @demo demo/index.html
* @polymerBehavior TSPolymerBehaviors.RemoveListenersBehavior
*/
window.TSPolymerBehaviors.RemoveListenersBehavior = {
ready() {
this._tsRegisteredEventListeners = [];
},
detached() {
this._tsRegisteredEventListeners.forEach(({target, type, listener}) => target.removeEventListener(type, listener));
this._tsRegisteredEventListeners = [];
},
/**
* Registers a new auto-removed listener on a target element.
*
* @param {Element} target The listener will be added to this element.
* @param {String} type The type of event to listen to.
* @param {Function} listener The listener to be added (and automatically removed on DOM detach).
*/
registerEventListener(target, type, listener) {
target.addEventListener(type, listener);
this._tsRegisteredEventListeners.push({target, type, listener});
}
};
})();