<foreachof>
The <foreachof>
tag creates reactive loops that dynamically update the DOM when the underlying data changes.
<foreachof a.jsvariable="value, index">
{(<div>${...}</div>)}
</foreachof>
<a-closure>
<let>
global watched g.items = ["Item 1", "Item 2", "Item 3"];
</let>
<foreachof a.jsvariable="g.items, idx">
{(<div key="${idx}">${g.items[idx]}</div>)}
</foreachof>
</a-closure>
singleton
The singleton
attribute ensures only one instance of an element with the same singleton name exists. Any new element with the same name will be automatically removed from the DOM before a.js does any parsing/execution.
<a-closure singleton="unique-component">
<div>Unique Content</div>
</a-closure>
singletonreplace
The singletonreplace
attribute allows replacing existing singletons.
<a-closure singleton="unique-component" singletonreplace>
<div>Replaced Content</div>
</a-closure>
waitFor
The waitFor
attribute delays the parsing and execution of an element until specified conditions are met. It uses a chain of selectors to resolve dependencies.
<a-closure waitFor='["my-layout", "#main", "my-page"]'>
<div>Dependent Content</div>
</a-closure>
my-layout
in the document.#main
within my-layout
.my-page
within #main
.The <import>
tag in a.js allows developers to dynamically include external JavaScript, CSS, or HTML resources into their projects, but it must be used within <a-xxxxx>
elements. This ensures that imports are scoped to the containing custom element, preventing unintended side effects on unrelated parts of the application.
<a-xxxxx>
element.A.importIn
function.<a-xxxxx>
element, influencing the final structure and behavior of the element.The <import>
tag uses the following attributes:
src
: Specifies the URL or path of the resource to import. Supports JavaScript (within <a-xxxxx>
elements), CSS, and HTML files.type
: Defines the type of the resource (e.g., "javascript"
, "css"
, or "html"
). Defaults to a script/text import (inside an <a-xxxxx>
element) if omitted.<a-closure>
<import src="./styles/main.css" type="css"></import> //import the styles in main.css in a way that only applies them to the children of currentElement
<import src="./scripts/utils.js" type="javascript"></import> //replace this <import></import> tag by the text of the ./scripts/utils.js file. If this text contains an import tag, it will be resolved too.
<import src="./components/header.html" type="html"></import> //import the content of ./components/header.html as child nodes of currentElement.
</a-closure>
Imports are the first elements to be processed inside an <a-xxxxx>
element. This means that they are parsed and executed before any other content or logic within the element.
This means it (usually) makes no sense to write something like
<a-closure>
currentElement.innerHTML = '<import src="./styles/dark-theme.css" type="css"></import>';
</a-closure>
Because at runtime, the <import>
will have been processed, and the script inside <a-closure>
will see the following:
<a-closure>
currentElement.innerHTML = '';
</a-closure>
When an imported resource references additional dependencies, a.js resolves these recursively. This ensures that all necessary resources are loaded before the application interacts with them.
If utils.js
contains <import src="./scripts/helpers.js"></import>
, the following code ensures both are loaded:
<a-closure>
<import src="./scripts/utils.js" type="javascript"></import>
</a-closure>
Dynamic resource loading can be accomplished using A.importIn
, which programmatically loads resources into a specified element.
A.importIn(currentElement, './html/layout.html', "html");
By importing HTML fragments as custom components, developers can keep their code modular and reusable.
<a-tagDef forTag="custom-card">
<import src="./components/card.tagdef"></import>
</a-tagDef>
<custom-card></custom-card>
Switching themes or applying styles dynamically based on user actions is simplified with A.importIn
.
<a-closure>
{(<button onclick="loadTheme('dark')">Dark Theme</button>)}
{(<button onclick="loadTheme('light')">Light Theme</button>)}
let loaded = null;
function loadTheme(theme) {
if (loaded) {
loaded.obj.remove();
}
A.importIn(currentElement, `./styles/${theme}-theme.css`, 'css',(elem,url,type,imports) => {
loaded = imports.css[0]
},true });
}
</a-closure>
<import>
tags to maintain performance.<a-xxxxx>
element.A.importIn
to load resources dynamically and optimize page load times.By using the <import>
tag effectively, developers can create highly modular, dynamic, and maintainable web applications with a.js.