Component
The properties, the methods and the delegates of a component instance.
On this page
A component instance is the first argument of the function given to COMPONENT(). It is created for each ui-component element. The Creating components guide shows how the parts work together.
API
property
id
The identifier of the instance.
idReturns String The identifier of the instance.
The value is generated, and the $id config key replaces it.
property
ID
The generated identifier of the instance.
IDReturns String The generated identifier of the instance.
The $id config key does not change it.
property
name
The name of the component.
nameReturns String The name of the component.
property
path
The path of the component, or null when the element has none.
pathReturns Path The path of the component, or null when the element has none.
See Path.
property
config
The current configuration.
configReturns Object The current configuration.
Change it with reconfigure(), so that configure is called.
property
element
The <ui-component> element as a jQuery object.
elementReturns jQuery The <ui-component> element as a jQuery object.
property
dom
The <ui-component> element.
domReturns HTMLElement The <ui-component> element.
property
plugin
The parent plugin, or null.
pluginReturns Plugin The parent plugin, or null.
See Plugin.
property
scope
The object that paths are resolved against, which is window.
scopeReturns Object The object that paths are resolved against, which is window.
property
cls
The CSS class name of the component, for example ui-grid.
clsReturns String The CSS class name of the component, for example ui-grid.
The class is not added to the element automatically.
property
def
A function that returns the value of the default attribute. It exists only when the attribute is used.
defReturns Function A function that returns the value of the default attribute. It exists only when the attribute is used.
property
ready
Is true once the instance starts to initialize.
readyReturns Boolean Is true once the instance starts to initialize.
delegate
init()
Runs once per declaration, for the first instance of the component.
init()It runs ahead of make. Use it for work shared by all instances.
<ui-component name="demo" path="common.value"></ui-component>
<script>
COMPONENT('demo', function(self) {
self.init = () => console.log('init');
self.make = () => console.log('make');
self.configure = key => console.log('configure', key);
self.setter = () => console.log('setter');
self.done = () => console.log('done');
});
// logs: init
// logs: make
// logs: setter
// logs: done
</script>
delegate
make()
Runs when the instance is created.
make()Build the content of the element here.
delegate
configure(key, value, init, prev)
Runs for each config key when the configuration changes.
configure(key, value, init, prev)| Parameter | Type | Description |
|---|---|---|
key |
String |
The config key. |
value |
Object |
The new value. |
init |
Boolean |
true during the initialization. |
prev |
Object |
The previous value, or null during the initialization. |
It runs for all keys when the component initializes.
<ui-component name="demo" config="title:Hello"></ui-component>
<script>
COMPONENT('demo', function(self) {
self.configure = function(key, value, init, prev) {
key === 'title' && console.log(value, !!init, prev);
};
self.make = () => setTimeout(() => self.reconfigure({ title: 'Bye' }), 100);
});
// logs: Hello true null
// logs: Bye false Hello
</script>
delegate
setter(value, path, flags)
Receives each change of the value at the path.
setter(value, path, flags)| Parameter | Type | Description |
|---|---|---|
value |
Object |
The current value. |
path |
String |
The path that changed. |
flags |
Object |
The flags of the change, for example { init: 1 }. |
The default implementation writes the value to the first input, textarea or select inside the element. A checkbox is checked when the value, as text, is true, 1, on, yes or ok. Assign your own function to render the value.
<ui-component name="demo" path="common.value"></ui-component>
<script>
COMPONENT('demo', function(self) {
self.setter = function(value, path, flags) {
console.log(value, path, JSON.stringify(flags));
};
});
SET('common.value @touched', 5);
// logs: 5 common.value {"touched":1}
</script>
delegate
setter2(value, path, flags)
Receives each change of the value, including the changes written with rewrite().
setter2(value, path, flags)| Parameter | Type | Description |
|---|---|---|
value |
Object |
The current value. |
path |
String |
The path that changed. |
flags |
Object |
The flags of the change. |
<ui-component name="demo" path="common.value"></ui-component>
<script>
COMPONENT('demo', function(self) {
window.com = self;
self.setter = value => value && console.log('setter', value);
self.setter2 = value => value && console.log('setter2', value);
});
com.rewrite(1);
// logs: setter2 1
</script>
delegate
getter(value, [realtime])
Processes a value entered by the user and writes it to the path.
getter(value, [realtime])| Parameter | Type | Description |
|---|---|---|
value |
Object |
The entered value. |
realtime? |
Boolean |
Writes the value without calling the setter. Default: false |
The value goes through the function set with parser() and is converted according to the type config key. With realtime, the value is written without calling the setter.
com.getter('5');
console.log(common.value);
// logs: 5
delegate
validate(value)
Decides whether the value is valid.
validate(value)| Parameter | Type | Description |
|---|---|---|
value |
Object |
The value to check. |
Returns Boolean The result of the validation. A string is also accepted.
Return true or an empty string for a valid value. A returned string is stored in config.invalid as the message, and any other result sets config.invalid to true. The method runs after each change of the value.
<ui-component name="demo" path="common.value"></ui-component>
<script>
COMPONENT('demo', function(self, config) {
window.com = self;
self.validate = value => value ? true : 'The value is required';
});
console.log(com.config.invalid);
// logs: The value is required
SET('common.value', 'x');
console.log(com.config.invalid);
// logs: false
</script>
delegate
state()
Runs when the state of the component changes.
state()Read the state from config.touched, config.modified, config.invalid and config.disabled.
<ui-component name="demo" path="common.value"></ui-component>
<script>
COMPONENT('demo', function(self, config) {
window.com = self;
self.state = () => console.log(!!config.touched);
});
com.touch();
// logs: true
</script>
delegate
state2(component)
Runs when another component with the same path reports its state.
state2(component)| Parameter | Type | Description |
|---|---|---|
component |
Component |
The component that reports the state. |
The report is made with validate2() or with the notify flag of bind().
<ui-component name="a" path="common.value"></ui-component>
<ui-component name="b" path="common.value"></ui-component>
<script>
COMPONENT('a', function(self) {
window.com = self;
});
COMPONENT('b', function(self) {
self.state2 = component => console.log(component.name);
});
com.validate2();
// logs: a
</script>
delegate
done()
Runs at the end of the initialization, when the value is set.
done()
delegate
destroy()
Runs when the instance is removed.
destroy()It is called by remove(), and by the regular cleanup for elements that left the page. Release timers and global handlers here.
<ui-component name="demo"></ui-component>
<script>
COMPONENT('demo', function(self) {
window.com = self;
self.destroy = () => console.log('destroyed');
});
com.remove();
// logs: destroyed
</script>
method
get()
Reads the value at the path of the component.
get()Returns Object The value, or null without a path.
SET('common.value', 1);
console.log(com.get());
// logs: 1
method
set(value, [flags])
Writes a value to the path and notifies everything bound to it.
set(value, [flags])| Parameter | Type | Description |
|---|---|---|
value |
Object |
The value to write. |
flags? |
String |
Flags such as @touched @modified. |
com.set(5, '@touched');
console.log(common.value);
// logs: 5
method
rewrite(value, [flags])
Writes a value to the path without calling the setter of this instance.
rewrite(value, [flags])| Parameter | Type | Description |
|---|---|---|
value |
Object |
The value to write. |
flags? |
String |
Flags such as @touched @modified. |
Other components and watchers are notified.
com.rewrite(7);
console.log(com.get());
// logs: 7
method
modify(value, [flags])
Writes a value and marks the component as touched and modified.
modify(value, [flags])| Parameter | Type | Description |
|---|---|---|
value |
Object |
The value to write. |
flags? |
String |
Flags such as @touched @modified. |
com.modify('x');
console.log(com.element.attr('class'));
// logs: ui-touched ui-modified
method
push(value)
Adds a value to the array at the path.
push(value)| Parameter | Type | Description |
|---|---|---|
value |
Object |
The value to write. |
An empty path is initialized with a new array and a single value is wrapped in an array.
com.push('a');
com.push('b');
console.log(common.value.join());
// logs: a,b
method
upd([flags])
Notifies everything bound to the path, without changing the value.
upd([flags])| Parameter | Type | Description |
|---|---|---|
flags? |
String |
Flags such as @touched @modified. |
The method is also available as update().
common.value = 5;
com.upd();
console.log(com.get());
// logs: 5
method
refresh()
Calls the setter with the current value.
refresh()<ui-component name="demo" path="common.value"></ui-component>
<script>
COMPONENT('demo', function(self) {
window.com = self;
self.setter = (value, path, flags) => flags.refresh && console.log('refresh');
});
com.refresh();
// logs: refresh
</script>
method
setPath(path)
Changes the path of the component.
setPath(path)| Parameter | Type | Description |
|---|---|---|
path |
String |
The new path. |
A ? is replaced with the path of the parent plugin. The setter is not called until the next change, so call refresh() to render the new value.
com.setPath('common.other');
console.log(com.path.toString());
// logs: common.other
method
makepath(path)
Replaces the ? in a path with the path of the parent plugin.
makepath(path)| Parameter | Type | Description |
|---|---|---|
path |
String |
The path. |
Returns String The resulting path.
<ui-plugin path="orders"><ui-component name="demo"></ui-component></ui-plugin>
<script>
COMPONENT('demo', function(self) {
self.make = () => console.log(self.makepath('?.items'));
});
// logs: orders.items
</script>
method
bind(flags, [value])
Changes the state of the component and optionally writes a value without calling the setter.
bind(flags, [value])| Parameter | Type | Description |
|---|---|---|
flags |
String |
The flags. |
value? |
Object |
The value to write. |
The flags are separated with a space, a comma or |, with or without @:
| Flag | Effect |
|---|---|
touched, touch | Marks the component as touched. |
modified, changed, modify, change | Marks the component as modified. |
reset | Clears the touched and modified state. |
validate | Runs the validation. |
notify | Calls state2 of the other components with the same path. |
setter | Calls the setter. |
com.bind('@touched @modified', 'x');
console.log(com.get(), com.element.attr('class'));
// logs: x ui-touched ui-modified
method
touch()
Marks the component as touched and runs the validation.
touch()com.touch();
console.log(com.config.touched);
// logs: 1
method
change()
Marks the component as touched and modified and runs the validation.
change()com.change();
console.log(com.element.attr('class'));
// logs: ui-touched ui-modified
method
reset([noemit])
Clears the touched and modified state.
reset([noemit])| Parameter | Type | Description |
|---|---|---|
noemit? |
Boolean |
Skips the validation. Default: false |
The validation runs unless noemit is true.
com.change();
com.reset();
console.log(com.config.touched, com.config.modified);
// logs: 0 0
method
isInvalid()
Tells whether the component is touched and invalid.
isInvalid()Returns Boolean A truthy value when the component is touched and invalid.
<ui-component name="demo" path="common.value"></ui-component>
<script>
COMPONENT('demo', function(self) {
window.com = self;
self.validate = value => !!value;
});
console.log(!!com.isInvalid());
// logs: false
com.touch();
console.log(!!com.isInvalid());
// logs: true
</script>
method
validate2([value])
Runs the validation and calls state2 of the other components with the same path.
validate2([value])| Parameter | Type | Description |
|---|---|---|
value? |
Object |
The value to validate. The default is the current value. |
method
reconfigure(value)
Changes the configuration and calls configure for each key.
reconfigure(value)| Parameter | Type | Description |
|---|---|---|
value |
Object |
The keys to change. |
The value is an object or a string in the form key:value;key:value. It accepts the special keys of the ui-component element, such as $class and =key.
com.reconfigure('title:Orders;limit:5');
console.log(com.config.title, com.config.limit);
// logs: Orders 5
method
watchconfig(key, path)
Keeps a config key equal to the value at a path.
watchconfig(key, path)| Parameter | Type | Description |
|---|---|---|
key |
String |
The config key, or null. |
path |
String |
The path to observe. |
With null as the key, the object at the path is applied as the configuration.
com.watchconfig('limit', 'common.limit');
SET('common.limit', 10);
console.log(com.config.limit);
// logs: 10
method
formatter(fn)
Sets a function that converts the value for display, or converts a value with it.
formatter(fn)| Parameter | Type | Description |
|---|---|---|
fn |
Function |
A function (path, value, type, format) that returns the value to display, or the value to convert. |
Returns Object The converted value, when a value is given.
With a function as the argument, the default setter uses it from then on. With any other value, the method returns the converted value.
<ui-component name="demo" path="common.value"><input /></ui-component>
<script>
COMPONENT('demo', function(self) {
self.formatter((path, value) => value ? '$' + value : '');
});
SET('common.value', 7);
console.log($('input').val());
// logs: $7
</script>
method
parser(fn)
Sets a function that converts the entered value for the model, or converts a value with it.
parser(fn)| Parameter | Type | Description |
|---|---|---|
fn |
Function |
A function (path, value, type, format) that returns the value to store, or the value to convert. |
Returns Object The converted value, when a value is given.
With a function as the argument, getter() uses it from then on. With any other value, the method returns the converted value.
<ui-component name="demo" path="common.value"></ui-component>
<script>
COMPONENT('demo', function(self) {
window.com = self;
self.parser((path, value) => value.replace('$', ''));
});
com.getter('$9');
console.log(common.value);
// logs: 9
</script>
method
autobind20([delay], [realtimebinding])
Connects the input, select and textarea inside the element with the path.
autobind20([delay], [realtimebinding])| Parameter | Type | Description |
|---|---|---|
delay? |
Number |
The delay in milliseconds. Default: 200 |
realtimebinding? |
Boolean |
Calls the setter also while the user types. Default: false |
The value is written after the delay, and the touched and modified states are updated from the user events.
<ui-component name="demo" path="common.value"><input /></ui-component>
<script>
COMPONENT('demo', function(self) {
self.make = () => self.autobind20(50);
});
$('input').val('typed').trigger('input');
setTimeout(() => console.log(common.value), 200);
// logs: typed
</script>
method
autofill([enabled])
Writes the values that the browser fills in on its own to the model.
autofill([enabled])| Parameter | Type | Description |
|---|---|---|
enabled? |
Boolean |
false turns the feature off. Default: true |
A browser can fill a form with saved data, such as a name or an address, without any input event. One second after the library starts, it reads the first input, textarea or select of each component that calls this method, and a value found there is written to the path with the flags @change and @init. The parser of the component converts the value first.
It works with the default setter. Components with a default attribute, select elements and range inputs are skipped. autofill(false) turns it off.
<ui-component name="login" path="form.email"><input type="email" /></ui-component>
<script>
COMPONENT('login', function(self) {
self.autofill();
});
// the browser fills the input with a saved address
setTimeout(() => $('input').val('ann@example.com'), 100);
setTimeout(() => console.log(GET('form.email')), 1500);
// logs: ann@example.com
</script>
method
readonly()
Turns the component into a display without getter, setter and validation.
readonly()
method
blind()
Excludes the component from notifications and from searches by path.
blind()
method
singleton()
Allows only one instance of the component.
singleton()Other <ui-component> elements with the same name are removed.
<ui-component name="demo"></ui-component>
<ui-component name="demo"></ui-component>
<script>
COMPONENT('demo', function(self) {
self.singleton();
});
console.log($('ui-component').length);
// logs: 1
</script>
method
watch(path, callback, [autoinit])
Calls a function on each change at a path.
watch(path, callback, [autoinit])| Parameter | Type | Description |
|---|---|---|
path |
String |
The path to observe. |
callback |
Function |
A function (value, flags, path). |
autoinit? |
Boolean |
Calls the function right away with the current value. Default: false |
Join several paths with +. With a function as the first argument, the path of the component is observed. A ? is replaced with the path of the parent plugin.
com.watch('common.user', (value, flags, path) => console.log(value, path));
SET('common.user', 'Anna');
// logs: Anna common.user
method
unwatch(path, [callback])
Removes watchers registered with watch().
unwatch(path, [callback])| Parameter | Type | Description |
|---|---|---|
path |
String |
The observed path. |
callback? |
Function |
The watcher to remove. |
Without callback, all the watchers of the path registered by this component are removed. With a function as the only argument, the watcher of the component path is removed. Join several paths with +. A ? is replaced with the path of the parent plugin.
var log = value => console.log('changed', value);
com.watch('common.count', log);
SET('common.count', 1);
com.unwatch('common.count', log);
SET('common.count', 2);
// logs: changed 1
method
datasource(path, callback, [init])
Observes a path with data for the component.
datasource(path, callback, [init])| Parameter | Type | Description |
|---|---|---|
path |
String |
The path to observe. |
callback |
Function |
A function (value, flags, path). |
init? |
Boolean |
Set false to skip the first call. Default: true |
Returns Component The instance.
The function is called with the current value when the component initializes, unless init is false, and on each change. A new call replaces the data source registered before, and datasource(null) removes it.
<ui-component name="demo"></ui-component>
<script>
var common = { items: ['a'] };
COMPONENT('demo', function(self) {
self.make = function() {
self.datasource('common.items', value => console.log(value.join()));
};
});
SET('common.items', ['a', 'b']);
// logs: a
// logs: a,b
</script>
method
on(name, callback)
Registers a handler of a global event.
on(name, callback)| Parameter | Type | Description |
|---|---|---|
name |
String |
The event name. |
callback |
Function |
The handler. |
Join several event names with +. See Events.
var handler = value => console.log(value);
com.on('refresh', handler);
EMIT('refresh', 1);
com.off('refresh', handler);
EMIT('refresh', 2);
// logs: 1
method
off(name, [callback])
Removes a handler of a global event.
off(name, [callback])| Parameter | Type | Description |
|---|---|---|
name |
String |
The event name. |
callback? |
Function |
The handler to remove. |
Without a callback, all handlers of the event registered on this instance are removed.
method
emit(name, [a], [b], [c], [d])
Emits a global event.
emit(name, [a], [b], [c], [d])| Parameter | Type | Description |
|---|---|---|
name |
String |
The event name. |
a? |
Object |
The first argument. |
b? |
Object |
The second argument. |
c? |
Object |
The third argument. |
d? |
Object |
The fourth argument. |
ON('saved', value => console.log(value));
com.emit('saved', 'ok');
// logs: ok
method
command(name, callback)
Registers a command of this instance.
command(name, callback)| Parameter | Type | Description |
|---|---|---|
name |
String |
The command name. |
callback |
Function |
The handler. |
com.command('clear', value => console.log('clear', value));
com.CMD('clear', 1);
// logs: clear 1
method
CMD(name, [a], [b], [c], [d], [e])
Runs a command registered on this instance with command().
CMD(name, [a], [b], [c], [d], [e])| Parameter | Type | Description |
|---|---|---|
name |
String |
The command name. |
a? |
Object |
The first argument. |
b? |
Object |
The second argument. |
c? |
Object |
The third argument. |
d? |
Object |
The fourth argument. |
e? |
Object |
The fifth argument. |
method
GET(path)
Reads a value from a path. A ? is replaced with the path of the parent plugin.
GET(path)| Parameter | Type | Description |
|---|---|---|
path |
String |
The path. |
Returns Object The value.
<ui-plugin path="orders"><ui-component name="demo"></ui-component></ui-plugin>
<script>
COMPONENT('demo', function(self) {
self.make = function() {
self.SET('?.count', 3);
console.log(self.GET('?.count'));
};
});
// logs: 3
</script>
method
SET(path, value)
Writes a value to a path and notifies everything bound to it. A ? is replaced with the path of the parent plugin.
SET(path, value)| Parameter | Type | Description |
|---|---|---|
path |
String |
The path. |
value |
Object |
The value to write. |
method
EXEC(name, [a], [b], [c], [d])
Calls a function or a plugin method.
EXEC(name, [a], [b], [c], [d])| Parameter | Type | Description |
|---|---|---|
name |
String |
For example FUNC.save, ?/save or @save. |
a? |
Object |
The first argument. |
b? |
Object |
The second argument. |
c? |
Object |
The third argument. |
d? |
Object |
The fourth argument. |
A ? is replaced with the path of the parent plugin. A name that starts with @ calls a method of the closest parent component.
<ui-component name="parent">
<ui-component name="child"></ui-component>
</ui-component>
<script>
COMPONENT('parent', function(self) {
self.refreshlist = value => console.log('parent', value);
});
COMPONENT('child', function(self) {
window.com = self;
});
com.EXEC('@refreshlist', 1);
// logs: parent 1
</script>
method
SEEX(name, [a], [b], [c], [d])
Calls a plugin method or a global function, or writes the first argument to the path.
SEEX(name, [a], [b], [c], [d])| Parameter | Type | Description |
|---|---|---|
name |
String |
The method, the function or the path. |
a? |
Object |
The first argument. |
b? |
Object |
The second argument. |
c? |
Object |
The third argument. |
d? |
Object |
The fourth argument. |
The form plugin/method calls a method of the plugin and a name of a global function calls that function. Any other name is a path that receives a.
com.SEEX('common.status', 'done');
console.log(common.status);
// logs: done
method
SETTER(name, [a], [b], [c], [d])
Calls a method of components in the whole page, in the form component/method.
SETTER(name, [a], [b], [c], [d])| Parameter | Type | Description |
|---|---|---|
name |
String |
The target, for example grid/refresh. |
a? |
Object |
The first argument. |
b? |
Object |
The second argument. |
c? |
Object |
The third argument. |
d? |
Object |
The fourth argument. |
<ui-component name="grid"></ui-component>
<ui-component name="demo"></ui-component>
<script>
COMPONENT('grid', function(self) {
self.reload = value => console.log('reload', value);
});
COMPONENT('demo', function(self) {
window.com = self;
});
com.SETTER('grid/reload', 1);
// logs: reload 1
</script>
method
find(selector)
Finds elements inside the component.
find(selector)| Parameter | Type | Description |
|---|---|---|
selector |
String |
A selector, or an object with selectors. |
Returns jQuery The found elements, or an object of them.
With an object, the method searches for each selector and returns an object with the same keys.
<ui-component name="demo"><input /><b></b></ui-component>
<script>
COMPONENT('demo', function(self) {
window.com = self;
});
console.log(com.find('input').length, com.find({ title: 'b' }).title.length);
// logs: 1 1
</script>
method
closest(selector)
Finds the closest parent that matches a selector.
closest(selector)| Parameter | Type | Description |
|---|---|---|
selector |
String |
A jQuery selector. |
Returns jQuery The found element.
console.log(com.closest('body')[0].tagName);
// logs: BODY
method
parent([selector])
Finds a parent element.
parent([selector])| Parameter | Type | Description |
|---|---|---|
selector? |
String |
The selector. |
Returns jQuery The found element.
Without an argument, the method returns the direct parent. These values are also supported:
| Selector | Result |
|---|---|
auto | The closest parent with a height set in its style, or window |
window | The window object as jQuery |
document | The document as jQuery |
parent followed by a number | The parent that many levels above the direct parent |
| anything else | The closest parent that matches the selector |
console.log(com.parent()[0].tagName);
// logs: BODY
method
html([value])
Reads or replaces the HTML of the element.
html([value])| Parameter | Type | Description |
|---|---|---|
value? |
String |
The new content. |
Returns jQuery The element, or the current HTML without an argument.
An array is joined into one string.
com.html(['<b>', 'Hello', '</b>']);
console.log(com.html());
// logs: <b>Hello</b>
method
text([value])
Reads or replaces the text of the element.
text([value])| Parameter | Type | Description |
|---|---|---|
value? |
String |
The new text. |
Returns jQuery The element, or the current text without an argument.
An array is joined into one string.
com.text('Hello');
console.log(com.text());
// logs: Hello
method
append(value)
Adds HTML to the end of the element.
append(value)| Parameter | Type | Description |
|---|---|---|
value |
String |
The content. |
Returns jQuery The element.
An array is joined into one string.
com.append('<i></i>');
com.append(['<b>', '</b>']);
console.log(com.html());
// logs: <i></i><b></b>
method
empty()
Removes the content of the element.
empty()Returns Component The instance.
method
attr(name, [value])
Reads or sets an attribute of the element.
attr(name, [value])| Parameter | Type | Description |
|---|---|---|
name |
String |
The attribute. |
value? |
String |
The new value. |
Returns Object The value when reading, the instance when setting.
com.attr('title', 'Hello');
com.attrd('id', 5);
console.log(com.attr('title'), com.attrd('id'));
// logs: Hello 5
method
attrd(name, [value])
Reads or sets a data- attribute of the element.
attrd(name, [value])| Parameter | Type | Description |
|---|---|---|
name |
String |
The name without the data- prefix. |
value? |
String |
The new value. |
Returns String The value when reading.
method
css(name, [value])
Reads or sets a CSS property of the element.
css(name, [value])| Parameter | Type | Description |
|---|---|---|
name |
String |
The property. |
value? |
String |
The new value. |
Returns Object The value when reading, the instance when setting.
com.css('color', 'red');
console.log(com.css('color'));
// logs: rgb(255, 0, 0)
method
aclass(name, [timeout])
Adds a CSS class to the element.
aclass(name, [timeout])| Parameter | Type | Description |
|---|---|---|
name |
String |
The class. |
timeout? |
Number |
A delay in milliseconds. |
Returns Component The instance.
com.aclass('a b');
com.rclass('a');
com.tclass('c');
console.log(com.element.attr('class'), com.hclass('c'));
// logs: b c true
method
rclass([name], [timeout])
Removes a CSS class from the element, or all classes without an argument.
rclass([name], [timeout])| Parameter | Type | Description |
|---|---|---|
name? |
String |
The class. |
timeout? |
Number |
A delay in milliseconds. |
Returns Component The instance.
method
rclass2(search)
Removes all CSS classes that contain a text.
rclass2(search)| Parameter | Type | Description |
|---|---|---|
search |
String |
The text to search for. |
Returns Component The instance.
com.aclass('size-1 size-2 box');
com.rclass2('size-');
console.log(com.element.attr('class'));
// logs: box
method
tclass(name, [enable])
Toggles a CSS class on the element.
tclass(name, [enable])| Parameter | Type | Description |
|---|---|---|
name |
String |
The class. |
enable? |
Boolean |
Forces the state of the class. |
Returns Component The instance.
method
hclass(name)
Tells whether the element has a CSS class.
hclass(name)| Parameter | Type | Description |
|---|---|---|
name |
String |
The class. |
Returns Boolean true when the class is present.
method
width()
Returns the width of the element in pixels.
width()Returns Number The width.
method
height()
Returns the height of the element in pixels.
height()Returns Number The height.
method
event(name, [selector], callback)
Registers a DOM event handler on the element.
event(name, [selector], callback)| Parameter | Type | Description |
|---|---|---|
name |
String |
The DOM event. |
selector? |
String |
Limits the event to matching children. |
callback |
Function |
The handler. |
Returns Component The instance.
The arguments are passed to the jQuery .on() method.
<ui-component name="demo"><button>Save</button></ui-component>
<script>
COMPONENT('demo', function(self) {
self.make = function() {
self.event('click', 'button', () => console.log('clicked'));
};
});
$('button').trigger('click');
// logs: clicked
</script>
method
autofocus([selector])
Moves the focus to the first visible control inside the element.
autofocus([selector])| Parameter | Type | Description |
|---|---|---|
selector? |
String |
The elements to focus. |
Returns Component The instance.
The default selector is input,select,textarea. When no control can take the focus yet, the method tries again every 200 milliseconds, 15 times at most. The focus is not moved on mobile devices.
<ui-component name="demo"><input id="first" /></ui-component>
<script>
COMPONENT('demo', function(self) {
window.com = self;
});
com.autofocus();
console.log(document.activeElement.id);
// logs: first
</script>
method
import(url, [callback], [prepare])
Downloads content and appends it to the element.
import(url, [callback], [prepare])| Parameter | Type | Description |
|---|---|---|
url |
String |
The address. |
callback? |
Function |
Runs when the content is in the page. |
prepare? |
Function |
A function (html) that returns the content to insert. |
See IMPORT() in Importing content.
com.import('/parts/help.html', () => console.log(com.html()), html => html + '<hr>');
// logs: <p>Help</p><hr>
method
replace(target, [remove])
Moves the component to another element.
replace(target, [remove])| Parameter | Type | Description |
|---|---|---|
target |
Element |
The new element, as a selector, a DOM element or jQuery. |
remove? |
Boolean |
Removes the original element. Default: false |
Returns Component The instance.
With remove, the original element is removed from the page.
com.replace('#box', true);
console.log(com.dom.id, $('ui-component').length);
// logs: box 0
method
remove()
Calls destroy and removes the component and its element.
remove()
method
icon(value)
Adds the icon prefix in DEF.iconprefix to an icon name.
icon(value)| Parameter | Type | Description |
|---|---|---|
value |
String |
The icon name. |
Returns String The CSS classes of the icon.
A value with a space already contains its prefix and stays unchanged.
console.log(com.icon('home'));
// logs: ti ti-home
console.log(com.icon('fa fa-home'));
// logs: fa fa-home
method
caniuse(name)
Finds the first declared component in a list of names separated with a comma or |.
caniuse(name)| Parameter | Type | Description |
|---|---|---|
name |
String |
The names. |
Returns String The found name, or false.
console.log(com.caniuse('missing,demo'));
// logs: demo
console.log(com.caniuse('missing|demo'));
// logs: demo
console.log(com.caniuse('missing'));
// logs: false
method
parseSource(value)
Parses a list in the form id|name|icon,id|name|icon.
parseSource(value)| Parameter | Type | Description |
|---|---|---|
value |
String |
The list. |
Returns Array Objects with id, name and icon.
The method is also available as parsesource(). When the type config key is number, the identifiers are converted to numbers.
var items = com.parseSource('1|One,2|Two|home');
console.log(JSON.stringify(items[1]));
// logs: {"id":"2","name":"Two","icon":"home"}