Skip to content

Commit 3bccc25

Browse files
author
Ludwig Schubert
committed
Register Controller listeners before tags customElements get registered.
1 parent a54f434 commit 3bccc25

File tree

1 file changed

+16
-15
lines changed

1 file changed

+16
-15
lines changed

src/components.js

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,20 @@ const distillMain = function() {
5050
console.info('Runlevel 1->2.');
5151
window.distillRunlevel += 1;
5252

53-
/* 3. Register components */
54-
/* Article will register controller which takes control from there */
53+
/* 3. Register Controller listener functions */
54+
/* Needs to happen before components to their connected callbacks have a controller to talk to. */
55+
for (const [functionName, callback] of Object.entries(Controller.listeners)) {
56+
if (typeof callback === 'function') {
57+
document.addEventListener(functionName, callback);
58+
} else {
59+
console.error('Runlevel 2: Controller listeners need to be functions!');
60+
}
61+
}
62+
console.info('Runlevel 2: We can now listen to controller events.');
63+
console.info('Runlevel 2->3.');
64+
window.distillRunlevel += 1;
65+
66+
/* 4. Register components */
5567
const components = [
5668
Abstract, Appendix, Article, Bibliography, Byline, Cite, CitationList, Code,
5769
Footnote, FootnoteList, FrontMatter, HoverBox, Title, DMath, References, TOC, Figure,
@@ -71,21 +83,10 @@ const distillMain = function() {
7183
customElements.define(component.is, component);
7284
}
7385

74-
console.info('Runlevel 2: Distill Template finished registering custom elements.');
75-
console.info('Runlevel 2->3.');
76-
window.distillRunlevel += 1;
77-
78-
/* 4. Register Controller listener functions */
79-
for (const [functionName, callback] of Object.entries(Controller.listeners)) {
80-
if (typeof callback === 'function') {
81-
document.addEventListener(functionName, callback);
82-
} else {
83-
console.error('Runlevel 3: Controller listeners need to be functions!');
84-
}
85-
}
86-
console.info('Runlevel 3: We can now listen to controller events.');
86+
console.info('Runlevel 3: Distill Template finished registering custom elements.');
8787
console.info('Runlevel 3->4.');
8888
window.distillRunlevel += 1;
89+
8990
// If template was added after DOMContentLoaded we may have missed that event.
9091
// Controller will check for that case, so trigger the event explicitly:
9192
Controller.listeners.DOMContentLoaded();

0 commit comments

Comments
 (0)