Lifecycle and teardown
Every vUX module is driven the same way: construct, configure, initialize, use, destroy. Once you know one module, you know how to start and stop all of them.
The four steps
import { Carousel } from "/lib/vUX/vUX-carousel.js";
const container = $$.ss("#hero");
const slides = new Carousel(container, container.querySelector(".slides")); // 1. construct: arguments checked at once
slides.config.delay = 4000; // 2. configure
slides.initialize(); // 3. initialize: the module starts working
// ...
slides.destroy(); // 4. destroy: everything it started is released- Construct with
new. The constructor checks its arguments and throws straight away if one is wrong, before anything touches the page. - Configure through
.config. Each property checks its value when you set it and throws on a bad one. - Call
initialize(). The module reads its configuration, builds what it needs and starts. On most modules, calling another method beforeinitialize()throws an error that tells you to initialize first. - Call
destroy()when it goes. The module removes the listeners, timers and animations it started and the DOM and stylesheets it injected. Afterdestroy(), create a new instance rather than reusing the old one.
.config is write-only
Configuration properties are setters. Reading one back gives undefined:
slides.config.delay = 4000;
slides.config.delay; // undefinedKeep your own copy of any setting you need to read later. Some modules let you change config after initialize() (Carousel, FontSplash); others read it once. Each reference page says which.
Errors name the problem
Validation errors are thrown synchronously and say which method or property was wrong and what it expected:
componentObj.mount(x, y) argument 1 must be an element (the container to mount into)
storeObj.set(x): 'usr' is not a field of this store; its fields are user, cart, totalEvery reference page ends with a list of the errors its module throws, and every example page has a section that triggers them on purpose.
Teardown in single-page apps
In an app built with SPA Engine, pages come and go without a reload, so anything a page started keeps running unless it is destroyed. Destroy the modules a page created when it is left:
- A Component destroys its instances, and an instance destroys the children mounted through it.
- A component's
onDestroycallback is the place to destroy anything you attached inonMount, such as a ToolTip. - A Store binding drops itself when its target is destroyed, but calling the function
bind()returned is the tidy way.
import { ToolTip } from "/lib/vUX/vUX-toolTip.js";
card.config.onMount = (instance) => {
instance.tip = new ToolTip();
instance.tip.config.className = "hint"; // required: elements with this class get the styled tip
instance.tip.initialize();
};
card.config.onDestroy = (instance) => instance.tip.destroy();