Spanish Total.js
English
Documentation For AI agents
Contents

ui-component

The attributes and the config keys of the ui-component element.

.md GitHub
On this page

The <ui-component> element places an instance of a component declared with COMPONENT() in the page. Its attributes are read once, when the element is created. The Using components guide explains how to work with it, and the instance itself is described on the Component page.

API

property

name="component"

The name of the component declared with COMPONENT(). The attribute is required: an element without it is ignored.

When no declaration with this name exists, the library downloads one from the address in the source attribute, or from the address in DEF.fallback.

A name that starts with LAZY (for example LAZY dialog) delays the creation of the instance until the first SETTER() call for that component.

Create a component on the first SETTER() call
<ui-component name="LAZY dialog"></ui-component>

<script>
COMPONENT('dialog', function(self) {
    console.log('created');
    self.show = function(title) {
        console.log('show', title);
    };
});

// The instance does not exist yet. This call creates it and then runs show().
SETTER('dialog/show', 'Hello');
// logs: created
// logs: show Hello
</script>
property

path="path"

The path of the value that the component reads and writes.

The value is passed to the setter of the component and every change at the path calls it again. A ? is replaced with the path of the parent plugin. Without this attribute, or with the value null, the component has no path. See Paths.

Bind a component to a path
<ui-component name="label" path="common.title"></ui-component>

<script>
COMPONENT('label', function(self) {
    self.setter = function(value) {
        self.text(value);
    };
});

SET('common.title', 'Hello');
console.log($('ui-component').text());
// logs: Hello
</script>
property

config="key:value;key:value"

The configuration of the instance.

Keys and values are separated with :, pairs with ;. Write \; and \: to use these characters in a value. The values true and false become booleans and numeric values become numbers. Everything else stays a string.

The keys of the attribute overwrite the default configuration given to COMPONENT().

Read the configuration
<ui-component name="box" config="title:Orders;limit:10;open:true;note:a\;b"></ui-component>

<script>
COMPONENT('box', 'limit:5;color:red', function(self, config) {
    console.log(JSON.stringify(config));
});
// logs: {"limit":10,"color":"red","title":"Orders","open":true,"note":"a;b"}
</script>
property

default="expression"

A JavaScript expression that gives the default value of the path.

The expression is evaluated when the component initializes and the value at the path is null or undefined. The result is written to the path without calling the setter a second time. It is evaluated again when a notification with the @default flag finds an empty value.

Fill an empty path
<ui-component name="field" path="common.count" default="10"></ui-component>
<ui-component name="field" path="common.tags" default="['a', 'b']"></ui-component>

<script>
COMPONENT('field', function(self) {});

console.log(common.count, common.tags.join());
// logs: 10 a,b
</script>
property

source="URL"

The address of the declaration, used when the component is not declared in the page.

The content at the address is imported once for all elements with the same name, and the waiting instances are created afterwards.

Load the declaration from your own server
<ui-component name="clock" source="/components/clock.html"></ui-component>
property

plugin="path"

Creates a plugin on the element itself.

The value has the same form as the path attribute of ui-plugin. The plugin is created first, and a ? in the path of the component refers to it.

A component with its own plugin
<ui-component name="total" plugin="orders" path="?.sum"></ui-component>

<script>
PLUGIN('orders', function(exports) {
    exports.make = function() {
        exports.set('sum', 25);
    };
});

COMPONENT('total', function(self) {
    self.setter = function(value) {
        self.text(value);
    };
    self.make = function() {
        console.log(self.path.toString(), self.plugin.name);
    };
});
// logs: orders.sum orders
</script>
config

$assign:path

Writes the component instance to a path.
Keep a reference to the instance
<ui-component name="grid" config="$assign:common.grid"></ui-component>

<script>
COMPONENT('grid', function(self) {
    self.reload = () => console.log('reloaded');
});

common.grid.reload();
// logs: reloaded
</script>
config

$class:name

Toggles a CSS class on the element when the component is configured.
Add a class once the component is ready
<ui-component name="panel" config="$class:ready"></ui-component>

<script>
COMPONENT('panel', function(self) {});
</script>
config

$id:value

Sets the id of the instance.

The identifier selects the component in SETTER('#value/method') and CONFIG('#value', ...).

Call one instance by its identifier
<ui-component name="tab" config="$id:first"></ui-component>
<ui-component name="tab" config="$id:second"></ui-component>

<script>
COMPONENT('tab', function(self) {
    self.open = () => console.log('open', self.id);
});

SETTER('#second/open');
// logs: open second
</script>
config

$init:path

Calls a function when the component is configured.

The function receives the instance. A ? in the value is replaced with the path of the parent plugin.

Run a function with the instance
<ui-component name="chart" config="$init:FUNC.chartready"></ui-component>

<script>
FUNC.chartready = function(component) {
    console.log(component.name);
};

COMPONENT('chart', function(self) {});
// logs: chart
</script>
config

$setter:path

Calls a function after each change of the value.

The function receives value, path and flags, the same arguments as the setter delegate.

Observe the value from outside
<ui-component name="input" path="common.name" config="$setter:FUNC.changed"></ui-component>

<script>
FUNC.changed = function(value, path) {
    value && console.log(path, value);
};

COMPONENT('input', function(self) {});

SET('common.name', 'Peter');
// logs: common.name Peter
</script>
config

=key:path

Keeps the config key key equal to the value at a path.

The key is set when the component initializes and on each change at the path. configure is called each time.

Drive a config key from the model
<ui-component name="list" config="=limit:common.limit"></ui-component>

<script>
COMPONENT('list', function(self) {
    self.configure = function(key, value) {
        key === 'limit' && value && console.log('limit', value);
    };
});

SET('common.limit', 50);
// logs: limit 50
</script>
config

=:path

Applies the object at a path as the configuration.

Each key of the object is set with reconfigure() when the component initializes and on each change at the path. The path must contain an object when the component initializes.

Configure a component from an object
<ui-component name="list" config="=:common.options"></ui-component>

<script>
var common = { options: { limit: 10 } };

COMPONENT('list', function(self, config) {
    self.make = () => setTimeout(() => console.log(config.limit), 50);
});
// logs: 10
</script>
config

type:name

Converts the value that the component writes with getter().

Supported values are number, currency, float, boolean, bool, date and datetime. Any other value leaves the input unchanged.

Store a number
<ui-component name="amount" path="common.price" config="type:number"><input /></ui-component>

<script>
COMPONENT('amount', function(self) {
    self.make = () => self.autobind20();
});

$('input').val('1 250.5').trigger('change');
setTimeout(() => console.log(common.price, typeof(common.price)), 300);
// logs: 1250.5 number
</script>
config

format:value

The date format used to parse the input when type is date or datetime.

Without this key, the format in DEF.dateformat is used.

Parse a custom date format
<ui-component name="date" path="common.date" config="type:date;format:dd.MM.yyyy"><input /></ui-component>

<script>
COMPONENT('date', function(self) {
    self.make = () => self.autobind20();
});

$('input').val('15.01.2024').trigger('change');
setTimeout(() => console.log(common.date.format('yyyy-MM-dd')), 300);
// logs: 2024-01-15
</script>
config

touched:true

Marks the component as touched and sets the ui-touched class on the element.
config

modified:true

Marks the component as modified and sets the ui-modified class on the element.
config

invalid:true

Marks the component as invalid and sets the ui-invalid class on the element.

The value is overwritten each time the component validates. When a validate delegate returns a string, the key holds that string.

config

disabled:true

Marks the component as disabled and sets the ui-disabled class on the element.
State classes
<ui-component name="field" config="disabled:true;touched:true"></ui-component>

<script>
COMPONENT('field', function(self) {});
</script>
config

required:true

Marks the component as required.

The key is set by the required command of ui-bind and is matched by the @required flag when components are searched by path. A component reads it in its own code.

Made in Europe