Application API
The global WildflowerJS application instance and its methods.
Script Tag Configuration
WildflowerJS automatically creates a global wildflower instance on load. You can configure it directly via script tag attributes:
<script src="wildflower.min.js" data-debug="true" data-error-handling="throw"></script>
| Attribute | Values | Default | Description |
|---|---|---|---|
data-debug |
true, "" (empty) |
false |
Enable debug mode and WILDFLOWER_DEBUG verbose logging |
data-error-handling |
log, throw, silent |
log |
How framework errors are handled |
data-auto-init |
true, false |
true |
Automatically scan and initialize components on page load |
data-wf-prefix |
true, false |
false |
Only process data-wf-* attributes (ignore data-*) |
Constructor
new WildflowerJS(root, options?)
Creates a new WildflowerJS application instance. Typically not needed since a global wildflower instance is created automatically.
Parameters
rootHTMLElement | Document | string - Root element, document, or selectoroptionsObject - Configuration options (optional)
Options
| Option | Type | Default | Description |
|---|---|---|---|
debug |
boolean | false | Enable debug logging |
autoInit |
boolean | true | Automatically scan and initialize components |
errorHandling |
'log' | 'throw' | 'silent' | 'log' | How to handle errors |
useWfPrefixOnly |
boolean | false | Only process data-wf-* attributes |
Example
// Most apps use the auto-created global instance
wildflower.component('my-component', { /* ... */ });
// Create custom instance (only if auto-init is disabled)
const app = new WildflowerJS('#app', {
debug: true,
errorHandling: 'throw'
});
component()
wildflower.component(name, definition)
Register a component definition.
Parameters
namestring - Unique component namedefinitionObject - Component configuration
Returns
WildflowerJS - The app instance (chainable)
Example
wildflower.component('my-component', {
state: {
message: 'Hello'
},
init() {
console.log('Component initialized');
},
updateMessage() {
this.message = 'Updated!';
}
});
getComponent()
wildflower.getComponent(componentId)
Get a component instance by its ID.
Parameters
componentIdstring - The component's unique ID
Returns
ComponentInstance | undefined
Example
const component = wildflower.getComponent('counter-1');
if (component) {
console.log(component.state.count);
}
getComponentsByType()
wildflower.getComponentsByType(name)
Get all instances of a component by type name.
Parameters
namestring - Component type name
Returns
ComponentInstance[]
Example
const counters = wildflower.getComponentsByType('counter');
counters.forEach(counter => {
counter.state.count = 0;
});
config()
wildflower.config(newOptions?)
Get or set framework configuration options at runtime. When called with an object, merges those options into the current configuration. Always returns the current configuration.
Parameters
newOptionsObject - Options to merge with current config (optional)
Returns
Object - A copy of the current configuration options
Example
// Get current config
const options = wildflower.config();
// Set config options
wildflower.config({ subscribeTimeout: 10000 });
scan()
wildflower.scan(scope?)
Manually scan a DOM scope for components to initialize. Useful after dynamically inserting HTML that contains component elements.
Parameters
scopestring | Element - Optional selector string or DOM element to limit scanning (defaults to document)
Returns
number - Count of newly initialized components
Example
// Scan entire document
wildflower.scan();
// Scan a specific container after inserting new HTML
container.innerHTML = '<div data-component="my-widget"></div>';
wildflower.scan(container);
getComponentInstance()
wildflower.getComponentInstance(componentId)
Get a single component instance by its internal ID.
Parameters
componentIdstring - The component's unique identifier
Returns
Object | undefined - The component instance, or undefined if not found
Example
const instance = wildflower.getComponentInstance('counter-1');
if (instance) {
console.log(instance.element);
}
hasComponentInstance()
wildflower.hasComponentInstance(componentId)
Check whether a component instance exists by its ID.
Parameters
componentIdstring - The component's unique identifier
Returns
boolean - True if the component instance exists
Example
if (wildflower.hasComponentInstance('counter-1')) {
wildflower.destroyComponent('counter-1');
}
destroyComponent()
wildflower.destroyComponent(componentId)
Destroy a single component instance and clean up all its resources. Calls lifecycle hooks, destroys child components, removes store subscriptions, and cleans up event handlers.
Parameters
componentIdstring - The unique identifier of the component to destroy
Returns
boolean - True if the component was found and destroyed, false otherwise
Example
const wasDestroyed = wildflower.destroyComponent('counter-1');
if (wasDestroyed) {
console.log('Component cleaned up');
}
setHtmlSanitizer()
wildflower.setHtmlSanitizer(fn)
Set a custom sanitizer function for data-bind-html content. When configured, all HTML rendered via data-bind-html is passed through this function before being set as innerHTML.
Parameters
fnFunction | null - Sanitizer function that accepts an HTML string and returns a sanitized HTML string, or null to disable sanitization
Example
// Using DOMPurify
wildflower.setHtmlSanitizer(html => DOMPurify.sanitize(html));
// Disable sanitization
wildflower.setHtmlSanitizer(null);
registerAdapter()
wildflower.registerAdapter(tagName, config)
Register an adapter for a custom element or web component. Adapters tell WildflowerJS how to read/write values and listen for changes on non-standard elements, enabling data-model and data-bind integration.
Parameters
tagNamestring - Custom element tag name (e.g.,'sl-input')configObject - Adapter configurationconfig.propstring - JS property name for reading/writing the element's value (default:'value')config.eventstring - Event name fired when the element's value changes (default:'input')
Example
// Shoelace input
wildflower.registerAdapter('sl-input', {
prop: 'value',
event: 'sl-input'
});
// Shoelace checkbox
wildflower.registerAdapter('sl-checkbox', {
prop: 'checked',
event: 'sl-change'
});
getAdapter()
wildflower.getAdapter(tagName, element?)
Retrieve the registered adapter for a given tag name. If no adapter is registered and an element instance is provided, auto-detects a sensible default for custom elements based on their capabilities.
Parameters
tagNamestring - Custom element tag nameelementHTMLElement - Optional element instance for auto-detection
Returns
Object | undefined - Adapter config ({ prop, event }), or undefined if no adapter found
Example
const adapter = wildflower.getAdapter('sl-input');
if (adapter) {
console.log(adapter.prop); // 'value'
console.log(adapter.event); // 'sl-input'
}
inspect()
wildflower.inspect(componentName?)
Inspect the running application or a specific component type. Logs a structured summary to the console and returns the data for programmatic use.
Parameters
componentNamestring - Optional. If provided, inspects all instances of that component type.
Returns
- No argument: { components, stores }, component counts and store state snapshots
- With component name: ComponentInstance[], array of matching instances
Example
// Full application summary
const app = wildflower.inspect();
app.stores.cart.total; // 149.97
app.components['nav-bar']; // 1
// Inspect a specific component type
const widgets = wildflower.inspect('kpi-widget');
widgets[0].element; // the DOM element
widgets[0].context; // the ContextProxy
destroy()
wildflower.destroy()
Completely destroy the framework instance and all components.
Example
// Clean up when done
wildflower.destroy();