Skip to content

Commit cacb743

Browse files
committed
feat(core basepattern): Throw pre-init.PATTERNNAME.patterns event.
Throw a bubbling pre-init.PATTERNNAME.patterns event before initializing the event for new class-based patterns.
1 parent e9a8f2f commit cacb743

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

src/core/basepattern.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ class BasePattern {
3131
}
3232
this.el = el;
3333

34+
// Notify pre-init
35+
this.el.dispatchEvent(
36+
new Event(`pre-init.${this.name}.patterns`, {
37+
bubbles: true,
38+
cancelable: true,
39+
})
40+
);
41+
3442
// Initialize asynchronously.
3543
//
3644
// 1) We need to call the concrete implementation of ``init``, but the

src/core/basepattern.test.js

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -210,20 +210,37 @@ describe("Basepattern class tests", function () {
210210
expect(cnt).toBe(1);
211211
});
212212

213-
it("6.2 - Throws a init event after asynchronous initialization has finished.", async function () {
213+
it("6.2 - Throws bubbling initialization events.", async function () {
214214
const events = (await import("./events")).default;
215215
class Pat extends BasePattern {
216216
static name = "example";
217217
static trigger = ".example";
218+
219+
async init() {
220+
this.el.dispatchEvent(new Event("initializing"), { bubbles: true });
221+
}
218222
}
219223

220-
const el = document.createElement("div");
224+
document.body.innerHTML = "<div></div>";
225+
const el = document.querySelector("div");
226+
227+
const event_list = [];
228+
document.body.addEventListener("pre-init.example.patterns", () =>
229+
event_list.push("pre-init.example.patterns")
230+
);
231+
document.body.addEventListener("pre-init.example.patterns", () =>
232+
event_list.push("initializing")
233+
);
234+
document.body.addEventListener("pre-init.example.patterns", () =>
235+
event_list.push("init.example.patterns")
236+
);
221237

222238
const pat = new Pat(el);
223239
await events.await_pattern_init(pat);
224240

225-
// If test reaches this expect statement, the init event catched.
226-
expect(true).toBe(true);
241+
expect(event_list[0]).toBe("pre-init.example.patterns");
242+
expect(event_list[1]).toBe("initializing");
243+
expect(event_list[2]).toBe("init.example.patterns");
227244
});
228245

229246
it("6.3 - Throws a not-init event in case of an double initialization event which is handled by await_pattern_init.", async function () {

0 commit comments

Comments
 (0)