A Global Object Methods and Functions

A.initialized()

Returns a promise that resolves once a.js has fully initialized, including defining all <a-xxxx> tags.

Syntax:

A.initialized().then(() => {
  console.log('a.js is fully initialized.');
});

Returns:


A.allParsed()

Returns a promise that resolves once all <a-xxxx> elements present at initialization have been parsed.

Syntax:

A.allParsed().then(() => {
  console.log('All a-elements have been parsed.');
});

Returns:


A.waitUntilAParsedIfExists(element)

Waits for a specified element to be parsed if it exists. If the element does not exist, the promise resolves immediately.

Parameters:

Returns:

Syntax:

A.waitUntilAParsedIfExists(document.querySelector('my-element')).then(() => {
  console.log('my-element parsed or does not exist.');
});

A.dumpAllStates()

Dumps all <let> variable states into a JSON string. This is useful for persisting the application state.

Returns:

Syntax:

const stateDump = A.dumpAllStates();
console.log(stateDump);

A.restoreAllStates(dump)

Restores <let> variable states from a previously dumped state string.

Parameters:

Syntax:

A.restoreAllStates(stateDump);

Behavior:


A.importIn(elem, url[, type][, removeAfterImport][, onloaded])

Imports content from the specified url into the given element elem by creating an <a-closure><import> in it. This can dynamically load HTML, CSS, JavaScript, or other resources.

Parameters:

Syntax:

A.importIn(currentElement, './fragment.html', 'html', true, () => {
  console.log('Import completed.');
});

A.getOrCreateKeyedElement(tagName, key, htmlElement[, namespace])

Retrieves or creates a keyed element associated with the provided key.

Parameters:

Returns:

Syntax:

const keyedElem = A.getOrCreateKeyedElement('div', 'unique-key', document.body);

A.removeKeyedElement(key, htmlElement[, namespace])

Removes a keyed element from the DOM and deletes its key association.

Parameters:

Returns:

Syntax:

A.removeKeyedElement('unique-key', document.body);

A.getAllKeyedElementsKeysArray(htmlElement[, namespace])

Retrieves an array of all keys associated with a specific element and namespace.

Parameters:

Returns:

Syntax:

const keys = A.getAllKeyedElementsKeysArray(document.body);
console.log(keys);

A.getAllKeyedElementsKeysSet(htmlElement[, namespace])

Retrieves an array of all keys associated with a specific element and namespace.

Parameters:

Returns:

Syntax:

const keys = A.getAllKeyedElementsKeysArray(document.body);
console.log(keys);

A.getAllKeyedElementsKeysObject(htmlElement[, namespace])

Retrieves an array of all keys associated with a specific element and namespace.

Parameters:

Returns:

Syntax:

const keys = A.getAllKeyedElementsKeysArray(document.body);
console.log(keys);

A.updateObject(target, source)

Merges the properties of the source object into the target object. Only differing properties are updated.

Parameters:

Returns:

Syntax:

let target = { a: 1, b: 2 };
let source = { b: 3, c: 4 };
A.updateObject(target, source);
console.log(target); // { a: 1, b: 3, c: 4 }

A.getJSONPath(obj, tpath[, value])

Retrieves or sets a value in an object or array using a JSON path.

Parameters:

Returns:

Syntax:

let data = { a: { b: [1, 2, 3] } };
const value = A.getJSONPath(data, 'a.b.1');
console.log(value); // value is 2 => logs 2

A.getJSONPath(data, 'a.b.1', 42);
console.log(data); // data has been modified => logs { a: { b: [1, 42, 3] } }

A.getNonATextContent(element)

Retrieves the content of an element, excluding everything that is an a-element or inside one.

Parameters:

Returns:

Syntax:

const text = A.getNonATextContent(document.querySelector('my-element'));
console.log(text);

A.getTextContent(element)

Retrieves the content of an element, including content inside <a-xxxx> elements but removes any <script type="a/unparsed"> and the corresponding closing tag.

Parameters:

Returns:

Syntax:

const text = A.getTextContent(document.querySelector('my-element'));
console.log(text);

A.getSourceContent(element)

Retrieves the content of an element, as it would appear when explored in the browser developer tools Elements tab.

Parameters:

Returns:

Syntax:

const source = A.getSourceContent(document.querySelector('my-element'));
console.log(source);

> Scoped variables