Skip to content

Commit e9a8f2f

Browse files
committed
feat(core base): Throw pre-init.PATTERNNAME.patterns event.
Throw a bubbling pre-init.PATTERNNAME.patterns event before initializing the event for old prototype based patterns.
1 parent 622d5e2 commit e9a8f2f

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

src/core/base.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ const Base = async function ($el, options, trigger) {
5656
this.$el = $el;
5757
this.el = $el[0];
5858
this.options = $.extend(true, {}, this.defaults || {}, options || {});
59+
60+
this.emit("pre-init");
61+
5962
await this.init($el, options, trigger);
6063

6164
// Store pattern instance on element

src/core/base.test.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -219,15 +219,21 @@ describe("pat-base: The Base class for patterns", function () {
219219
const node = document.createElement("div");
220220
node.setAttribute("class", "pat-example");
221221
const event_list = [];
222-
node.addEventListener("init_done", () => event_list.push("pat init"));
223-
$(node).on("init.example.patterns", () => event_list.push("base init"));
222+
$(node).on("pre-init.example.patterns", () =>
223+
event_list.push("pre-init.example.patterns")
224+
);
225+
node.addEventListener("init_done", () => event_list.push("init_done"));
226+
$(node).on("init.example.patterns", () =>
227+
event_list.push("init.example.patterns")
228+
);
224229
new Tmp(node);
225230

226231
// await until all asyncs are settled. 1 event loop should be enough.
227232
await utils.timeout(1);
228233

229-
expect(event_list[0]).toBe("pat init");
230-
expect(event_list[1]).toBe("base init");
234+
expect(event_list[0]).toBe("pre-init.example.patterns");
235+
expect(event_list[1]).toBe("init_done");
236+
expect(event_list[2]).toBe("init.example.patterns");
231237
});
232238

233239
it("adds the pattern instance on the element when manually initialized", async () => {

0 commit comments

Comments
 (0)