-
Notifications
You must be signed in to change notification settings - Fork 5
/
55_custom_events.js
128 lines (117 loc) · 4.23 KB
/
55_custom_events.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/**
* ========================================================
* Custom Events
* ========================================================
* Custom Events enable you to define your own events in JavaScript.
* This is very powerful for building modular, decoupled code.
*/
/**
* ========================================================
* 1. Creating a Custom Event
* ========================================================
* Create a Custom Event using the 'CustomEvent' constructor.
* The 'detail' field allows you to pass custom data for the event.
*/
const myEvent = new CustomEvent("myEvent", {
detail: { message: "This is a custom event" },
bubbles: true,
cancelable: true,
});
/**
* ========================================================
* 2. Dispatching a Custom Event
* ========================================================
* Dispatch the custom event using the 'dispatchEvent' method on an HTML element.
* This will trigger any listeners for the custom event.
*/
const someElement = document.getElementById("someElement");
someElement.dispatchEvent(myEvent);
/**
* ========================================================
* 3. Listening for a Custom Event
* ========================================================
* Use 'addEventListener' to listen to the custom event.
* Event handling is similar to handling native DOM events.
*/
someElement.addEventListener("myEvent", (event) => {
console.log("Custom event fired:", event.detail.message);
});
/**
* ========================================================
* 4. Removing an Event Listener
* ========================================================
* To stop listening to an event, use 'removeEventListener'.
*/
function someFunction(event) {
console.log("Handled by someFunction:", event.detail.message);
}
someElement.removeEventListener("myEvent", someFunction);
/**
* ========================================================
* Nuances and Advanced Techniques
* ========================================================
*/
/**
* 1. Using Custom Events with Components
* --------------------------------------
* Custom Events are highly useful in component-based architectures like Web Components.
*/
class MyComponent extends HTMLElement {
connectedCallback() {
this.dispatchEvent(new CustomEvent("componentReady", { bubbles: true }));
}
}
customElements.define("my-component", MyComponent);
/**
* 2. Custom Events with Additional Data
* -------------------------------------
* You can attach extra information to a custom event via the 'detail' attribute.
*/
const userDataEvent = new CustomEvent("userData", {
detail: { username: "IshtmeetDoe", age: 30 },
});
/**
* 3. Event Propagation and Bubbling
* ----------------------------------
* You can control the phase in which the event is captured.
* Use 'true' as the third argument to 'addEventListener' to capture the event during the capture phase.
*/
someElement.addEventListener(
"myEvent",
function (event) {
console.log("Parent received:", event.detail.message);
},
true
);
/**
* 4. Cancellable Custom Events
* ------------------------------
* A custom event can be made cancellable, meaning it can be stopped by an event listener.
*/
const cancellableEvent = new CustomEvent("cancellableEvent", { cancelable: true });
if (!someElement.dispatchEvent(cancellableEvent)) {
console.log("Event was cancelled");
}
/**
* 5. Polyfill for CustomEvent
* ----------------------------
* For compatibility with older browsers like Internet Explorer, you can use a polyfill.
*/
(function () {
if (typeof window.CustomEvent === "function") return false;
function CustomEvent(event, params) {
params = params || { bubbles: false, cancelable: false, detail: null };
const evt = document.createEvent("CustomEvent");
evt.initCustomEvent(event, params.bubbles, params.cancelable, params.detail);
return evt;
}
window.CustomEvent = CustomEvent;
})();
/**
* 6. Dynamic Event Names
* ------------------------
* Dynamic event names can be generated and used, providing an extra layer of flexibility.
*/
const someDynamicValue = "Click";
const eventName = "custom:" + someDynamicValue;
someElement.addEventListener(eventName, someFunction);