Umbraco Controller
Contain or reuse logic across Elements. A Controller enables you to separate logic while still being connected with the life cycle of an element.
Host Element
Element Life Cycle
Registration
import { UmbControllerBase } from '@umbraco-cms/backoffice/class-api';
import type { UmbControllerHost } from '@umbraco-cms/backoffice/controller-api';
export class MyOwnControllerImplementation extends UmbControllerBase {
#secondsAlive = 0;
constructor(host: UmbControllerHost) {
// Parse the host to the base class, this will trigger the registration.
super(host);
}
hostConnected() {
// It's important to call the super method when overriding a method of the base class.
super.hostConnected();
this.#timer = setInterval(this.#onInterval, 1000)
}
hostDisconnected() {
// It's important to call the super method when overriding a method of the base class.
super.hostDisconnected();
clearInterval(this.#timer);
}
#onInterval = () => {
this.#secondsAlive++;
console.log(`My own controller have been connected in ${this.#secondsAlive} seconds.`);
}
override destroy(): void {
// It's important to call the super method when overriding a method of the base class.
super.destroy();
// We do not need to stop the timer in the Destroy method, because the hostDisconnected method is also called if connected and destroyed.
}
}Last updated
Was this helpful?