ui-component
The attributes, configuration keys, methods and CSS classes of the ui-component element.
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.
name="component"The attribute accepts these forms:
| Value | Meaning |
|---|---|
box | The component declared as box. |
box@2 | The component declared as box@2. |
box|panel | The first of the listed components that is declared. |
box,panel | One instance of each component on the same element. |
lazy box | The instance is created on the first SETTER() call for box. |
An element without name is ignored.
<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>
<ui-component name="missing|hello"></ui-component>
<script>
COMPONENT('hello', function(self) {
console.log(self.name); // hello
});
</script>
<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.
path="path"The value of the path is passed to the setter delegate. The attribute accepts these forms:
| Value | Meaning |
|---|---|
form.name | An absolute path. |
?.name | A path relative to the closest ui-plugin. |
? | The path of the closest <ui-plugin>. |
*name | A path inside DEF.pathcommon, which is common. by default. |
%name | A temporary path. |
@key | Reads the value stored with data() of the parent component. |
form.name --> expression | Transforms 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.
<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>
<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>
<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.
config="key:value;key:value"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().
<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.
config="=key:path"The value of =key:path is read from the path. When the path changes, the instance receives the new value through the configure delegate.
<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.
config="=path"The path must contain an object. The instance is reconfigured each time the path changes.
<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.
config="%variable"The instance works directly with the object stored in window.
<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.
config="key:@xs=value,@lg=value"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.
<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.
$id:identifierThe identifier is stored in id and can be used in selectors such as #users.
<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.
$type:typeThe type is stored in type and is passed to parsers and formatters.
<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.
$class:classes<ui-component name="box" config="$class:highlighted"></ui-component>
<script>COMPONENT('box', function(self) {});</script>
config
$assign:path
Writes the instance to a path.
$assign:pathThe path is set to null when the instance is destroyed.
<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.
$init:pathThe function receives the instance, and this is the instance too.
<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.
$setter:pathThe function receives value, path and type, and this is the instance.
<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.
$state:pathThe function receives type and what, the same arguments as the state delegate.
<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>.
$noscope:trueThe ? character in the path is not replaced.
<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.
$bindvisible:trueIt has the same effect as calling bindvisible() in the declaration.
<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.
$url:urlThe URL is passed to IMPORT(). It runs once per URL.
<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.
$realtime:falseWith true the value is written while the user types. With false it is written on the change event.
<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.
$delay:millisecondsThe default value is DEF.delaykeypress, which is 200.
<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.
default="expression"The attribute contains a JavaScript expression. The value is written when the path is undefined, and default() restores it.
<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.
plugin="path"The value is the path of the scope. It works like wrapping the element in a ui-plugin.
<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.
data-jc-path="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.
data-jc-config="key:value"<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.
data-jc-class="classes"Separate several classes with a space.
<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>.
data-jc-noscope="true"It has the same effect as the $noscope key.
<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.
element.get()Returns Object The value of the path.
var element = document.querySelector('ui-component');
console.log(element.get()); // Peter
method
element.set(value, [type])
Writes a value to the bound path.
element.set(value, [type])| Parameter | Type | Description |
|---|---|---|
value |
Object |
The new value. |
type? |
Number |
The type of the update passed to the setter. |
The instance is marked as modified.
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.
element.update([type])| Parameter | Type | Description |
|---|---|---|
type? |
Number |
The type of the update passed to the setter. |
The instance is marked as modified.
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.
element.modify(value, [type])| Parameter | Type | Description |
|---|---|---|
value |
Object |
The new value. |
type? |
Number |
The type of the update. Default: 2 |
It calls modify() of the instance.
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.
element.reconfigure([value], [init], [noemit])| Parameter | Type | Description |
|---|---|---|
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.
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.
element.reset()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.
element.configThe property exists when the instance has been created.
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.
element.setter = function(value, path, type)Assign it to the element. It runs after the setter delegate of the instance with the same arguments.
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.
element.state = function(config)The function receives the configuration object of the instance.
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.
element.configure = function(key, value, init, prev)The function receives the same arguments as the configure delegate.
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.
$(element).on('component', callback)The event is triggered once.
<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.
.ui-modifiedIt follows config.modified.
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.
.ui-touchedIt follows config.touched.
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.
.ui-invalidIt follows config.invalid, which is set by the validate delegate.
<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.
.ui-disabledIt follows config.disabled. The disabled command of <ui-bind> sets the key.
<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>