Resource
A base resource abstraction for creating, connecting and observing DOM-loaded assets.
import { Resource } from "@schukai/monster/source/dom/resource.mjs";Resource is the shared base contract for DOM-loaded assets such as scripts, links and data tags. It owns the common lifecycle: create an element, connect it into the document and observe whether it has loaded or failed.
Use it when you need a reusable loading abstraction instead of manually creating elements in feature code. Most applications work with derived resource classes, not with the base class directly.
Create And Connect A DOM Resource
import {
referenceSymbol,
Resource,
} from "@schukai/monster/source/dom/resource.mjs";
class DemoResource extends Resource {
static getURLAttribute() {
return "src";
}
create() {
const element = document.createElement("div");
element.textContent = `resource: ${this.getOption("src")}`;
element.className = "monster-card";
this[referenceSymbol] = element;
return this;
}
}
const output = document.getElementById("resource-output");
document.getElementById("resource-run").addEventListener("click", () => {
const resource = new DemoResource({
src: "/demo.json",
document,
query: "#resource-output",
});
resource.connect();
output.textContent = JSON.stringify(
{
source: resource.getOption("src"),
connected: resource.isConnected(),
},
null,
2,
);
});Exported
Resource, KEY_DOCUMENT, KEY_QUERY, referenceSymbolDerived from
BaseWithOptionsOptions
The Options listed in this section are defined directly within the class. This class is derived from several parent classes. Therefore, it inherits Options from these parent classes. If you cannot find a specific Options in this list, we recommend consulting the documentation of the BaseWithOptions.
Properties
The Properties listed in this section are defined directly within the class. This class is derived from several parent classes. Therefore, it inherits Properties from these parent classes. If you cannot find a specific Properties in this list, we recommend consulting the documentation of the BaseWithOptions.
Methods
The methods listed in this section are defined directly within the class. This class is derived from several parent classes. Therefore, it inherits methods from these parent classes. If you cannot find a specific method in this list, we recommend consulting the documentation of the BaseWithOptions.
Constructor
constructor(options)options{object|undefined}: options
Behavioral methods
create()- {Monster.DOM.Resource}
{Error}this method must be implemented by derived classes
connect().State query methods
isConnected()- {boolean}
Static methods
[instanceSymbol]()- {symbol}
getURLAttribute()- {string}
Other methods
available()- {Promise}
available() you can check if a resource is available. This is the case when the tag is included and the resource is loaded.connect()- {Monster.DOM.Resource}
create() is called implicitly. throws {Error} target not foundEvents
This component does not fire any public events. It may fire events that are inherited from its parent classes.