Skip to content

Commit 89ccff3

Browse files
committed
Initial prototype for lifecycle events.
1 parent 9f78a0d commit 89ccff3

File tree

5 files changed

+50
-9
lines changed

5 files changed

+50
-9
lines changed

django_unicorn/static/unicorn/js/component.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
addDbEventListener,
66
addModelEventListener,
77
} from "./eventListeners.js";
8-
import { components } from "./store.js";
8+
import { components, lifecycleEvents } from "./store.js";
99
import { send } from "./messageSender.js";
1010
import morphdom from "./morphdom/2.6.1/morphdom.js";
1111
import {
@@ -517,4 +517,10 @@ export class Component {
517517
debounce(send, debounceTime, false)(this, callback);
518518
}
519519
}
520+
521+
triggerLifecycleEvent(eventName) {
522+
if (eventName in lifecycleEvents) {
523+
lifecycleEvents[eventName].forEach((cb) => cb(this));
524+
}
525+
}
520526
}

django_unicorn/static/unicorn/js/messageSender.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,8 @@ export function send(component, callback) {
215215
);
216216
}
217217

218+
component.triggerLifecycleEvent("updated");
219+
218220
// Re-init to refresh the root and checksum based on the new data
219221
component.init();
220222

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,15 @@
1+
/**
2+
* Stores components.
3+
*
4+
* Key is the component id.
5+
* Value is the instantiated component.
6+
*/
17
export const components = {};
8+
9+
/**
10+
* Stores lifecycle events to fire.
11+
*
12+
* Key is the event name.
13+
* Value is an array of callback functions.
14+
*/
15+
export const lifecycleEvents = {};

django_unicorn/static/unicorn/js/unicorn.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Component } from "./component.js";
22
import { isEmpty, hasValue } from "./utils.js";
3-
import { components } from "./store.js";
3+
import { components, lifecycleEvents } from "./store.js";
44

55
let messageUrl = "";
66
let csrfTokenHeaderName = "X-CSRFToken";
@@ -108,3 +108,16 @@ export function getReturnValue(componentNameOrKey) {
108108

109109
return component.return.value;
110110
}
111+
112+
/**
113+
* Adds an event listener for particular events.
114+
* @param {String} eventName The event to register under. Current events are: `updated`.
115+
* @param {Function} callback Function to call when the event gets fired.
116+
*/
117+
export function addEventListener(eventName, callback) {
118+
if (!(eventName in lifecycleEvents)) {
119+
lifecycleEvents[eventName] = [];
120+
}
121+
122+
lifecycleEvents[eventName].push(callback);
123+
}
Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
<div>
2-
<button unicorn:click="$reset">Reset the component</button>
3-
<br />
4-
<br />
2+
<button unicorn:click="$reset">Reset the component</button>
3+
<br />
4+
<br />
55

6-
<label></label>
7-
<input unicorn:model="name" type="text" id="name" unicorn:key="key1">
6+
<label></label>
7+
<input unicorn:model="name" type="text" id="name" unicorn:key="key1">
88

9-
Hello, {{ name|default:'World' }}!
10-
</div>
9+
Hello, {{ name|default:'World' }}!
10+
</div>
11+
12+
<script type="application/javascript">
13+
window.addEventListener('DOMContentLoaded', (event) => {
14+
Unicorn.addEventListener('updated', (component) => console.log('got updated', component));
15+
});
16+
</script>

0 commit comments

Comments
 (0)