Spanish Total.js
English
Documentation For AI agents
Contents

ui-component

The attributes, configuration keys, methods and CSS classes of the ui-component element.

.md GitHub
On this page

The <ui-component> element places a component declared with COMPONENT() in the page. Its attributes select the component, bind it to a path and configure it. Using components explains how to work with the element, and the instance it creates is described in Component.

API

property

name="component"

Selects the component to create.

The attribute accepts these forms:

ValueMeaning
boxThe component declared as box.
box@2The component declared as box@2.
box|panelThe first of the listed components that is declared.
box,panelOne instance of each component on the same element.
lazy boxThe instance is created on the first SETTER() call for box.

An element without name is ignored.

A component bound to a path
<ui-component name="hello" path="common.user"></ui-component>

<script>
    COMPONENT('hello', function(self) {
        self.setter = function(value) {
            self.text('Hello ' + value);
        };
    });
    SET('common.user', 'Peter');
</script>
The first declared component wins
<ui-component name="missing|hello"></ui-component>

<script>
    COMPONENT('hello', function(self) {
        console.log(self.name);  // hello
    });
</script>
A lazy component
<ui-component name="lazy dialog"></ui-component>

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

    // The first SETTER() call creates the instance
    setTimeout(() => SETTER('dialog/show', 'Welcome'), 500);
</script>
property

path="path"

Binds the component to a path of the data model.

The value of the path is passed to the setter delegate. The attribute accepts these forms:

ValueMeaning
form.nameAn absolute path.
?.nameA path relative to the closest ui-plugin.
?The path of the closest <ui-plugin>.
*nameA path inside DEF.pathcommon, which is common. by default.
%nameA temporary path.
@keyReads the value stored with data() of the parent component.
form.name --> expressionTransforms the value with an expression that receives value.

A component without path is not bound to the data model. More about the syntax is in Paths.

Path forms
<ui-plugin path="users">
    <ui-component name="show" path="?.name"></ui-component>
</ui-plugin>
<ui-component name="show" path="*title"></ui-component>

<script>
    PLUGIN('users', function(exports) {});
    COMPONENT('show', function(self) {
        console.log(self.path);
    });
</script>
Transforming the value
<ui-component name="upper" path="form.name --> (value || '').toUpperCase()"></ui-component>

<script>
    COMPONENT('upper', function(self) {
        self.setter = function(value) {
            self.text(value);
        };
    });
    SET('form.name', 'peter');
</script>
Reading data of the parent component
<ui-component name="card">
    <ui-component name="label" path="@title"></ui-component>
</ui-component>

<script>
    COMPONENT('card', function(self) {
        self.make = function() {
            self.data('title', 'Orders');
        };
    });
    COMPONENT('label', function(self) {
        self.setter = function(value) {
            self.text(value);
        };
    });
    setTimeout(() => FIND('card').data('title', 'Invoices'), 500);
</script>
property

config="key:value;key:value"

Sets the initial configuration of the instance.

The value is a list of key:value pairs separated by semicolons. Numbers and booleans are converted to their types. The attribute is read once, when the instance is created. To change the configuration later, call reconfigure().

The configuration is merged in this order: the default of COMPONENT(), this attribute and then the rules of CONFIG().

Typed values
<ui-component name="box" config="title:Orders;limit:10;visible:true"></ui-component>

<script>
    COMPONENT('box', 'limit:5;color:red', function(self, config) {
        console.log(config.title, config.limit, config.visible, config.color);  // Orders 10 true red
    });
</script>
type

config="=key:path"

A key that starts with = reads its value from a path and follows its changes.

The value of =key:path is read from the path. When the path changes, the instance receives the new value through the configure delegate.

A watched key
<ui-component name="box" config="=title:common.title"></ui-component>

<script>
    SET('common.title', 'First');
    COMPONENT('box', function(self, config) {
        self.configure = function(key, value) {
            key === 'title' && self.text(value);
        };
    });
    setTimeout(() => SET('common.title', 'Second'), 500);
</script>
type

config="=path"

A value that starts with = and has no colon loads the whole configuration from a path.

The path must contain an object. The instance is reconfigured each time the path changes.

Reading an object
<ui-component name="box" config="=common.options"></ui-component>

<script>
    SET('common.options', { title: 'Orders' });
    COMPONENT('box', function(self, config) {
        self.configure = function(key, value) {
            key === 'title' && self.text(value);
        };
    });
    setTimeout(() => SET('common.options', { title: 'Invoices' }), 500);
</script>
type

config="%variable"

A value that starts with % uses a global variable as the configuration object.

The instance works directly with the object stored in window.

Using a global object
<ui-component name="box" config="%boxoptions"></ui-component>

<script>
    window.boxoptions = { title: 'Orders' };
    COMPONENT('box', function(self, config) {
        console.log(config.title, config === window.boxoptions);  // Orders true
    });
</script>
type

config="key:@xs=value,@lg=value"

A value can differ per display size or per light and dark appearance.

Write the alternatives as @name=value separated by commas. The names are xs, sm, md, lg, dark and light. The value that matches the current display is used. If the current size has no value, the closest defined size is used.

A value per display size
<ui-component name="grid" config="columns:@xs=1,@sm=2,@md=3,@lg=4"></ui-component>

<script>
    COMPONENT('grid', function(self, config) {
        console.log(WIDTH(), config.columns);  // xs 1
    });
</script>
config

$id:identifier

Sets the identifier of the instance.

The identifier is stored in id and can be used in selectors such as #users.

Finding an instance by its identifier
<ui-component name="box" config="$id:users"></ui-component>

<script>
    COMPONENT('box', function(self) {});
    setTimeout(() => console.log(FIND('#users').name), 500);  // box
</script>
config

$type:type

Sets the data type of the instance.

The type is stored in type and is passed to parsers and formatters.

Setting the type
<ui-component name="box" config="$type:number"></ui-component>

<script>
    COMPONENT('box', function(self) {
        self.make = () => console.log(self.type);  // number
    });
</script>
config

$class:classes

Toggles CSS classes on the element.
Adding a class
<ui-component name="box" config="$class:highlighted"></ui-component>
<script>COMPONENT('box', function(self) {});</script>
config

$assign:path

Writes the instance to a path.

The path is set to null when the instance is destroyed.

Keeping a reference
<ui-component name="box" config="$assign:common.box"></ui-component>

<script>
    COMPONENT('box', function(self) {});
    setTimeout(() => console.log(GET('common.box').name), 500);  // box
</script>
config

$init:path

Calls a function when the instance is ready.

The function receives the instance, and this is the instance too.

An initialization function
<ui-component name="box" config="$init:common.onbox"></ui-component>

<script>
    window.common = { onbox: instance => console.log('ready', instance.name) };  // ready box
    COMPONENT('box', function(self) {});
</script>
config

$setter:path

Calls a function each time the instance receives a value.

The function receives value, path and type, and this is the instance.

Observing values
<ui-component name="box" path="form.name" config="$setter:common.onvalue"></ui-component>

<script>
    window.common = { onvalue: value => value && console.log(value) };  // Peter
    COMPONENT('box', function(self) {
        self.setter = function() {};
    });
    SET('form.name', 'Peter');
</script>
config

$state:path

Calls a function each time the state of the instance changes.

The function receives type and what, the same arguments as the state delegate.

Observing the state
<ui-component name="box" config="$state:common.onstate"></ui-component>

<script>
    window.common = { onstate: (type, what) => console.log(type, what) };  // 0 3
    COMPONENT('box', function(self) {});
</script>
config

$noscope:true

Keeps the path outside the closest <ui-plugin>.

The ? character in the path is not replaced.

Ignoring the scope
<ui-plugin path="users">
    <ui-component name="box" path="?.name" config="$noscope:true"></ui-component>
</ui-plugin>

<script>
    PLUGIN('users', function(exports) {});
    COMPONENT('box', function(self) {
        console.log(self.path);  // ?.name
    });
</script>
config

$bindvisible:true

Delays values until the element is visible.

It has the same effect as calling bindvisible() in the declaration.

Waiting for a visible element
<div id="tab" style="display:none">
    <ui-component name="box" path="form.name" config="$bindvisible:true"></ui-component>
</div>

<script>
    COMPONENT('box', function(self) {
        self.setter = function(value) {
            value && console.log('received', value);  // received Peter
        };
    });
    SET('form.name', 'Peter');
    setTimeout(() => $('#tab').show(), 500);
</script>
config

$url:url

Imports the component when it is not declared.

The URL is passed to IMPORT(). It runs once per URL.

Importing a missing component
<ui-component name="remote" config="$url:/components/remote.html"></ui-component>
config

$realtime:false

Selects when an input with data-jc-bind writes its value.

With true the value is written while the user types. With false it is written on the change event.

Writing on change
<ui-component name="box" path="form.name" config="$realtime:false">
    <input data-jc-bind />
</ui-component>

<script>
    COMPONENT('box', function(self) {});
    setTimeout(function() {
        var input = $('input').val('Peter').trigger('input');
        setTimeout(function() {
            console.log(GET('form.name') || 'empty');  // empty
            input.trigger('change');
            console.log(GET('form.name'));  // Peter
        }, 300);
    }, 500);
</script>
config

$delay:milliseconds

Sets the delay of realtime binding in milliseconds.

The default value is DEF.delaykeypress, which is 200.

A shorter delay
<ui-component name="box" path="form.name" config="$delay:20">
    <input data-jc-bind />
</ui-component>

<script>
    COMPONENT('box', function(self) {});
    setTimeout(function() {
        $('input').val('Peter').trigger('input');
        setTimeout(() => console.log(GET('form.name')), 60);  // Peter
    }, 500);
</script>
property

default="expression"

Sets the default value of the path.

The attribute contains a JavaScript expression. The value is written when the path is undefined, and default() restores it.

A default value
<ui-component name="box" path="form.name" default="'Anonymous'"></ui-component>

<script>
    COMPONENT('box', function(self) {
        self.setter = function() {};
    });
    setTimeout(() => console.log(GET('form.name')), 500);  // Anonymous
</script>
property

plugin="path"

Creates a scope directly on the element.

The value is the path of the scope. It works like wrapping the element in a ui-plugin.

A scope on the element
<ui-component name="box" plugin="users" path="?.name"></ui-component>

<script>
    PLUGIN('users', function(exports) {});
    COMPONENT('box', function(self) {
        console.log(self.path);  // users.name
    });
</script>
property

data-jc-path="path"

Sets the path and takes priority over the path attribute.
Overriding the path
<ui-component name="box" path="form.a" data-jc-path="form.b"></ui-component>

<script>
    COMPONENT('box', function(self) {
        console.log(self.path);  // form.b
    });
</script>
property

data-jc-config="key:value"

Sets the configuration and takes priority over the config attribute.
Overriding the configuration
<ui-component name="box" config="title:A" data-jc-config="title:B"></ui-component>

<script>
    COMPONENT('box', function(self, config) {
        console.log(config.title);  // B
    });
</script>
property

data-jc-class="classes"

Toggles CSS classes when the instance is ready.

Separate several classes with a space.

A class added after the initialization
<ui-component name="box" data-jc-class="ready"></ui-component>
<script>COMPONENT('box', function(self) {});</script>
property

data-jc-noscope="true"

Keeps the path outside the closest <ui-plugin>.

It has the same effect as the $noscope key.

Ignoring the scope
<ui-plugin path="users">
    <ui-component name="box" path="?.name" data-jc-noscope="true"></ui-component>
</ui-plugin>

<script>
    PLUGIN('users', function(exports) {});
    COMPONENT('box', function(self) {
        console.log(self.path);  // ?.name
    });
</script>
method

element.get()

Returns the value of the bound path.

Returns Object The value of the path.

Reading the value
var element = document.querySelector('ui-component');
console.log(element.get());  // Peter
method

element.set(value, [type])

Writes a value to the bound path.
ParameterTypeDescription
value Object The new value.
type? Number The type of the update passed to the setter.

The instance is marked as modified.

Writing a value
var element = document.querySelector('ui-component');
element.set('Anna');
console.log(GET('form.name'));  // Anna
method

element.update([type])

Notifies the bound path with its current value.
ParameterTypeDescription
type? Number The type of the update passed to the setter.

The instance is marked as modified.

Refreshing after a direct change
var element = document.querySelector('ui-component');
window.form.name = 'Anna';
element.update();
setTimeout(() => console.log(element.textContent), 50);  // Anna
method

element.modify(value, [type])

Writes a value through the parsers of the instance.
ParameterTypeDescription
value Object The new value.
type? Number The type of the update. Default: 2

It calls modify() of the instance.

Writing a parsed value
var element = document.querySelector('ui-component');
element.modify('  Anna  ');
console.log(GET('form.name'));  // Anna
method

element.reconfigure([value], [init], [noemit])

Changes the configuration of the instance.
ParameterTypeDescription
value? String|Object The new configuration.
init? Boolean Passes init to the configure delegate.
noemit? Boolean Skips the configure delegate.

Without a value, the method reads the config attribute again.

Changing a key
var element = document.querySelector('ui-component');
element.reconfigure('title:Invoices');
console.log(element.config.title);  // Invoices
method

element.reset()

Clears the invalid and modified state of the instance.
Clearing the state
var element = document.querySelector('ui-component');
element.set('Anna');
element.reset();
console.log(element.config.modified);  // false
property

element.config

The configuration object of the instance.

The property exists when the instance has been created.

Reading the configuration
var element = document.querySelector('ui-component');
console.log(element.config.title);  // Orders
delegate

element.setter = function(value, path, type)

A function on the DOM element that receives each value.

Assign it to the element. It runs after the setter delegate of the instance with the same arguments.

Observing values from outside
var element = document.querySelector('ui-component');
element.setter = function(value, path, type) {
    console.log(value, path, type);  // Anna form.name 1
};
SET('form.name', 'Anna');
delegate

element.state = function(config)

A function on the DOM element that runs when the state changes.

The function receives the configuration object of the instance.

Observing the state from outside
var element = document.querySelector('ui-component');
element.state = function(config) {
    console.log(config.touched);  // true
};
FIND('demo').bind('touched');
delegate

element.configure = function(key, value, init, prev)

A function on the DOM element that receives configuration changes.

The function receives the same arguments as the configure delegate.

Observing the configuration
var element = document.querySelector('ui-component');
element.configure = function(key, value, init, prev) {
    console.log(key, value, prev);  // title Invoices Orders
};
element.reconfigure('title:Invoices');
event

$(element).on('component', callback)

A jQuery event triggered on the element when the instance is ready.

The event is triggered once.

Waiting for the instance
<ui-component name="box" id="box"></ui-component>

<script>
    COMPONENT('box', function(self) {});
    $('#box').on('component', () => console.log('ready'));  // ready
</script>
class

.ui-modified

Present while the value of the instance is modified.

It follows config.modified.

The modified class
var com = FIND('demo');
com.bind('modified');
console.log(com.element.hclass('ui-modified'));  // true
class

.ui-touched

Present after the user has interacted with the instance.

It follows config.touched.

The touched class
var com = FIND('demo');
com.bind('touched');
console.log(com.element.hclass('ui-touched'));  // true
class

.ui-invalid

Present while the value of the instance is invalid.

It follows config.invalid, which is set by the validate delegate.

The invalid class
<ui-component name="box" path="form.name"></ui-component>

<script>
    COMPONENT('box', function(self) {
        self.validate = value => !!value;
    });
</script>
class

.ui-disabled

Present while the instance is disabled.

It follows config.disabled. The disabled command of <ui-bind> sets the key.

The disabled class
<ui-bind path="form.locked" config="disabled:value">
    <ui-component name="box"></ui-component>
</ui-bind>

<script>
    COMPONENT('box', function(self) {});
    SET('form.locked', true);
</script>
Made in Europe