Component
The delegates, properties and methods of a component instance.
On this page
A component instance is the object passed to the declaration of COMPONENT(). It connects an element to a path of the data model. Creating components explains how to write one, and ui-component describes the element. In the examples, com is an instance found with FIND().
API
delegate
self.make = function()
Builds the content of the component.
self.make = function()The delegate runs once, after the declaration, when the element is in the DOM.
<ui-component name="box"></ui-component>
<script>
COMPONENT('box', function(self) {
self.make = function() {
self.html('<b>Ready</b>');
};
});
</script>
delegate
self.init = function()
Runs once for all instances of the component.
self.init = function()Only the first instance of the page calls this delegate.
<ui-component name="box"></ui-component>
<ui-component name="box"></ui-component>
<script>
COMPONENT('box', function(self) {
self.init = () => console.log('once'); // once
});
</script>
delegate
self.done = function()
Runs after the instance has been initialized.
self.done = function()The delegate runs after the first setter and state calls.
<ui-component name="box"></ui-component>
<script>
COMPONENT('box', function(self) {
self.done = () => console.log('done'); // done
});
</script>
delegate
self.released = function(hidden)
Tells whether the element is hidden when the instance starts.
self.released = function(hidden)| Parameter | Type | Description |
|---|---|---|
hidden |
Boolean |
true when the element is hidden. |
The delegate runs once and receives true for a hidden element.
<ui-component name="box"></ui-component>
<script>
COMPONENT('box', function(self) {
self.released = hidden => console.log(hidden); // false
});
</script>
delegate
self.configure = function(key, value, init, prev)
Receives the configuration keys.
self.configure = function(key, value, init, prev)| Parameter | Type | Description |
|---|---|---|
key |
String |
The name of the key. |
value |
Object |
The new value. |
init |
Boolean |
true during the initialization. |
prev |
Object |
The previous value. |
The delegate runs for each key during the initialization, with init set to true, and again for each changed key. It also receives changes of the modified, touched and invalid keys.
<ui-component name="box" config="title:Orders"></ui-component>
<script>
COMPONENT('box', function(self) {
self.configure = function(key, value, init, prev) {
key === 'title' && console.log(value, init); // Orders true
};
});
</script>
delegate
self.setter = function(value, path, type)
Receives the value of the path.
self.setter = function(value, path, type)| Parameter | Type | Description |
|---|---|---|
value |
Object |
The value of the path. |
path |
String |
The path that changed. |
type |
Number |
The source of the change. |
Define it to render the value. The default implementation writes the formatted value to nested input, select and textarea fields with the data-jc-bind attribute.
The type argument tells where the change comes from:
| Type | Source |
|---|---|
0 | The initialization. |
1 | A manual change, such as SET(). |
2 | An input of the user. |
3 | A default value or a reset. |
<ui-component name="box" path="form.name"></ui-component>
<script>
COMPONENT('box', function(self) {
self.setter = function(value, path, type) {
self.text(value);
};
});
SET('form.name', 'Peter');
</script>
delegate
self.getter(value, [realtime], [nobind])
Writes a value from the user to the path.
self.getter(value, [realtime], [nobind])| Parameter | Type | Description |
|---|---|---|
value |
Object |
The raw value. |
realtime? |
Boolean |
true when the value comes while the user types. |
nobind? |
Boolean |
true validates the value without writing it. |
The default implementation runs the parsers and writes the result. Nested fields with the data-jc-bind attribute call it automatically. Call it from your own controls to write a value.
<ui-component name="box" path="form.name"><button>Set</button></ui-component>
<script>
COMPONENT('box', function(self) {
self.make = function() {
self.event('click', 'button', () => self.getter('Peter'));
};
});
setTimeout(function() {
$('button').click();
console.log(GET('form.name')); // Peter
}, 500);
</script>
delegate
self.setter2 = function(value, path, type)
Runs after the setter delegate.
self.setter2 = function(value, path, type)It receives the same arguments. Use it to extend a component and keep its setter.
<ui-component name="box" path="form.name"></ui-component>
<script>
COMPONENT('box', function(self) {
self.setter2 = value => value && console.log(value); // Peter
});
SET('form.name', 'Peter');
</script>
delegate
self.getter2 = function(value, realtime)
Runs when the getter receives a value.
self.getter2 = function(value, realtime)It receives the parsed value and the realtime argument.
<ui-component name="box" path="form.name"></ui-component>
<script>
COMPONENT('box', function(self) {
self.getter2 = value => console.log(value); // PETER
self.parser((path, value) => value.toUpperCase());
});
setTimeout(() => FIND('box').getter('peter'), 500);
</script>
delegate
self.state = function(type, what)
Runs when the state of the instance changes.
self.state = function(type, what)| Parameter | Type | Description |
|---|---|---|
type |
Number |
The source of the change. |
what |
Number |
The kind of the change. |
The type argument is 0 for the initialization, 1 for a manual change and 2 for an input of the user.
The what argument tells what happened:
| What | Meaning |
|---|---|
1 | Validation. |
2 | The touched state changed. |
3 | A reset. |
4 | An update. |
5 | A new value. |
6 | A notification. |
<ui-component name="box" path="form.name"></ui-component>
<script>
COMPONENT('box', function(self) {
self.state = function(type, what) {
console.log(type, what); // 0 3
};
});
</script>
delegate
self.state2 = function(type, what)
Runs after the state delegate.
self.state2 = function(type, what)It receives the same arguments.
<ui-component name="box" path="form.name"></ui-component>
<script>
COMPONENT('box', function(self) {
self.state2 = (type, what) => console.log(type, what); // 0 3
});
</script>
delegate
self.validate = function(value, init)
Decides whether the value is valid.
self.validate = function(value, init)| Parameter | Type | Description |
|---|---|---|
value |
Object |
The value of the path. |
init? |
Boolean |
true during the initialization. |
Return true for a valid value. The result is stored in config.invalid.
<ui-component name="box" path="form.name"></ui-component>
<script>
COMPONENT('box', function(self, config) {
self.validate = value => !!value;
self.done = () => console.log(config.invalid); // true
});
</script>
delegate
self.service = function(counter)
Runs once per minute.
self.service = function(counter)| Parameter | Type | Description |
|---|---|---|
counter |
Number |
The number of runs. |
The delegate receives a counter that grows by one on each run.
delegate
self.destroy = function()
Runs when the instance is removed.
self.destroy = function()Release timers and external listeners here.
<ui-component name="box"></ui-component>
<script>
COMPONENT('box', function(self) {
self.destroy = () => console.log('destroyed'); // destroyed
});
setTimeout(() => FIND('box').remove(), 500);
</script>
property
self.name
The name of the component, with its version.
self.name<ui-component name="box@2"></ui-component>
<script>
COMPONENT('box@2', self => console.log(self.name)); // box@2
</script>
property
self.version
The version from the name of the component.
self.versionThe value is empty when the name has no version. The element also receives the jc-v{version} class.
<ui-component name="box@2"></ui-component>
<script>
COMPONENT('box@2', self => console.log(self.version)); // 2
</script>
property
self.id
The identifier of the instance.
self.idThe $id configuration key sets it. Otherwise it has the same value as ID.
<ui-component name="box" config="$id:orders"></ui-component>
<script>
COMPONENT('box', self => self.make = () => console.log(self.id)); // orders
</script>
property
self.ID
The unique generated identifier of the instance.
self.ID<ui-component name="box"></ui-component>
<script>
COMPONENT('box', self => console.log(self.ID.substring(0, 2))); // jc
</script>
property
self.path
The path of the data model bound to the instance.
self.pathThe value is empty when the component has no path.
<ui-component name="box" path="form.name"></ui-component>
<script>
COMPONENT('box', self => console.log(self.path)); // form.name
</script>
property
self.type
The data type of the instance.
self.typeThe value is passed to parsers and formatters. Set it in the declaration or with the $type key.
<ui-component name="box"></ui-component>
<script>
COMPONENT('box', function(self) {
self.type = 'number';
self.parser((path, value, type) => console.log(type)); // number
self.make = () => self.parser('1');
});
</script>
property
self.format
The format passed to parsers and formatters.
self.formatWhen the property is empty, config.format is used.
<ui-component name="box"></ui-component>
<script>
COMPONENT('box', function(self) {
self.format = 'dd.MM.yyyy';
self.formatter((path, value, type, format) => console.log(format)); // dd.MM.yyyy
self.make = () => self.formatter('1');
});
</script>
property
self.trim
Removes spaces around text in the parser.
self.trimThe default value is true.
<ui-component name="box" path="form.name"></ui-component>
<script>
COMPONENT('box', function(self) {
self.trim = false;
self.make = () => console.log(self.parser(' A ').length); // 5
});
</script>
property
self.config
The configuration object.
self.configIt also holds the state keys modified, touched, invalid and, when set, disabled.
<ui-component name="box" config="title:Orders"></ui-component>
<script>
COMPONENT('box', self => console.log(self.config.title, self.config.touched)); // Orders false
</script>
property
self.element
The element of the instance as a jQuery object.
self.element<ui-component name="box"></ui-component>
<script>
COMPONENT('box', self => console.log(self.element.prop('tagName'))); // UI-COMPONENT
</script>
property
self.dom
The element of the instance as a DOM node.
self.dom<ui-component name="box"></ui-component>
<script>
COMPONENT('box', self => console.log(self.dom.tagName)); // UI-COMPONENT
</script>
property
self.owner
The closest parent component.
self.ownerThe property is missing when the component has no parent component.
<ui-component name="parent"><ui-component name="child"></ui-component></ui-component>
<script>
COMPONENT('parent', function(self) {});
COMPONENT('child', self => console.log(self.owner.name)); // parent
</script>
property
self.caller
The component that called this instance through exec().
self.caller<ui-component name="parent"><ui-component name="child"></ui-component></ui-component>
<script>
COMPONENT('parent', function(self) {});
COMPONENT('child', function(self) {
self.ping = () => console.log(self.caller.name); // parent
});
setTimeout(() => FIND('parent').exec('ping'), 500);
</script>
property
self.global
An object shared by all instances of the component.
self.global<ui-component name="box"></ui-component>
<ui-component name="box"></ui-component>
<script>
COMPONENT('box', function(self) {
self.global.count = (self.global.count || 0) + 1;
});
setTimeout(() => console.log(FIND('box').global.count), 500); // 2
</script>
property
self.scope
The Scope of the instance.
self.scopeThe property exists when the path contains ?.
<ui-plugin path="users"><ui-component name="box" path="?.name"></ui-component></ui-plugin>
<script>
PLUGIN('users', function(exports) {});
COMPONENT('box', self => console.log(self.scope.path)); // users
</script>
property
self.pathscope
The path of the scope of the instance.
self.pathscopeThe property exists when the path contains ?.
<ui-plugin path="users"><ui-component name="box" path="?.name"></ui-component></ui-plugin>
<script>
PLUGIN('users', function(exports) {});
COMPONENT('box', self => console.log(self.pathscope)); // users
</script>
property
self.plugin
The Plugin of the scope of the instance.
self.pluginThe property exists when the path contains ?.
<ui-plugin path="users"><ui-component name="box" path="?.name"></ui-component></ui-plugin>
<script>
PLUGIN('users', function(exports) {});
COMPONENT('box', self => console.log(self.plugin.name)); // users
</script>
property
self.siblings
Tells whether the element hosts several components.
self.siblings<ui-component name="a,b"></ui-component>
<script>
COMPONENT('a', self => console.log(self.siblings)); // true
COMPONENT('b', function(self) {});
</script>
property
self.removed
Tells whether the instance has been removed.
self.removed<ui-component name="box"></ui-component>
<script>
COMPONENT('box', function(self) {});
setTimeout(function() {
var com = FIND('box');
com.remove();
console.log(com.removed); // true
}, 500);
</script>
method
com.get([path])
Returns the value of the bound path.
com.get([path])| Parameter | Type | Description |
|---|---|---|
path? |
String |
Another path. |
Returns Object The value.
With an argument, the method reads another path.
var com = FIND('demo');
SET('form.name', 'Peter');
console.log(com.get()); // Peter
method
com.set(value, [type])
Writes a value to the bound path.
com.set(value, [type])| Parameter | Type | Description |
|---|---|---|
value |
Object |
The new value. |
type? |
Number |
The type passed to the setter. |
Returns Component The instance.
The instance is marked as modified.
var com = FIND('demo');
com.set('Peter');
console.log(GET('form.name'), com.config.modified); // Peter true
method
com.modify(value, [type])
Writes a value through the parsers.
com.modify(value, [type])| Parameter | Type | Description |
|---|---|---|
value |
Object |
The raw value. |
type? |
Number |
The type passed to the setter. Default: 2 |
Returns Component The instance.
The instance is marked as touched.
var com = FIND('demo');
com.modify(' Peter ');
console.log(GET('form.name'), com.config.touched); // Peter true
method
com.inc(value, [type])
Increases the number stored in the bound path.
com.inc(value, [type])| Parameter | Type | Description |
|---|---|---|
value |
Number |
The increment. |
type? |
Number |
The type passed to the setter. |
Returns Component The instance.
var com = FIND('demo');
com.set(5);
com.inc(2);
console.log(GET('form.name')); // 7
method
com.extend(value, [type])
Merges an object into the bound path.
com.extend(value, [type])| Parameter | Type | Description |
|---|---|---|
value |
Object |
The fields to merge. |
type? |
Number |
The type passed to the setter. |
Returns Component The instance.
var com = FIND('demo');
com.set({ a: 1 });
com.extend({ b: 2 });
console.log(GET('form.name')); // {"a":1,"b":2}
method
com.push(value, [type])
Adds an item to the array stored in the bound path.
com.push(value, [type])| Parameter | Type | Description |
|---|---|---|
value |
Object |
The item. |
type? |
Number |
The type passed to the setter. |
Returns Component The instance.
var com = FIND('demo');
com.push('A');
com.push('B');
console.log(GET('form.name')); // ["A","B"]
method
com.rewrite(value, [type])
Writes a value without notifying anyone.
com.rewrite(value, [type])| Parameter | Type | Description |
|---|---|---|
value |
Object |
The new value. |
type? |
Number |
The type of the change. |
Returns Component The instance.
Bound components, binders and watchers are not called.
var com = FIND('demo');
com.rewrite('Peter');
console.log(GET('form.name')); // Peter
method
com.update([type])
Writes the current value again.
com.update([type])| Parameter | Type | Description |
|---|---|---|
type? |
Number |
The type passed to the setter. |
Returns Component The instance.
All listeners of the path are notified.
var com = FIND('demo');
com.watch(function(path, value) {
console.log(value); // Peter
});
window.form.name = 'Peter';
com.update();
method
com.refresh([notify], [type])
Calls the setter of this instance with the current value.
com.refresh([notify], [type])| Parameter | Type | Description |
|---|---|---|
notify? |
Boolean |
true notifies all listeners. |
type? |
Number|String |
The type passed to the setter. |
Returns Component The instance.
With notify, the value is written to the path and all listeners are notified.
var com = FIND('demo');
window.form.name = 'Peter';
com.refresh();
console.log(com.text()); // Peter
method
com.notify()
Notifies the listeners of the exact bound path.
com.notify()Returns Component The instance.
The method calls NOTIFY() with the path of the instance.
var com = FIND('demo');
window.form.name = 'Peter';
com.notify();
console.log(com.text()); // Peter
method
com.skip()
Skips the next call of the setter.
com.skip()Returns Component The instance.
var com = FIND('demo');
com.skip();
com.set('Peter');
setTimeout(() => console.log(com.text() || 'empty'), 500); // empty
method
com.bind(flags, [value])
Changes the state and optionally writes a value.
com.bind(flags, [value])| Parameter | Type | Description |
|---|---|---|
flags |
String |
The flags. |
value? |
Object |
The value to write. |
The flags argument is a list of words separated by a space, a comma or |. An optional @ prefix is ignored.
| Flag | Effect |
|---|---|
touched, touch | Marks the instance as touched. |
modified, modify, changed, change | Marks the instance as modified. |
reset | Clears the touched and modified state. |
validate | Validates the value. |
setter | Calls the setter. |
When value is passed, it is written to the path, and the setter of this instance runs only with the setter flag.
var com = FIND('demo');
com.bind('@touched @modified', 'Peter');
console.log(GET('form.name'), com.config.touched, com.config.modified); // Peter true true
method
com.reset()
Clears the touched and modified state.
com.reset()Returns Component The instance.
The value is validated again.
var com = FIND('demo');
com.modify('Peter');
com.reset();
console.log(com.config.touched, com.config.modified); // false false
method
com.default([reset])
Restores the default value of the instance.
com.default([reset])| Parameter | Type | Description |
|---|---|---|
reset? |
Boolean |
false keeps the state of the instance. Default: true |
Returns Component The instance.
The default value comes from the default attribute.
var com = FIND('demo');
com.set('Peter');
com.default();
console.log(GET('form.name')); // Anonymous
method
com.change([value])
Sets the touched and modified state of components on the path.
com.change([value])| Parameter | Type | Description |
|---|---|---|
value? |
Boolean |
The new state. Default: true |
Returns Component The instance.
The method applies to all components bound to the path and its nested paths. The method is also available as touched().
var com = FIND('demo');
com.change();
console.log(com.config.touched, com.config.modified); // true true
method
com.dirty([value], [noemit])
Reads or sets the untouched state.
com.dirty([value], [noemit])| Parameter | Type | Description |
|---|---|---|
value? |
Boolean |
false marks the instance as touched. |
noemit? |
Boolean |
true skips the state delegate. |
Returns Boolean|Component The state, or the instance when value is passed.
Without arguments, the method returns true for an untouched instance.
var com = FIND('demo');
console.log(com.dirty()); // true
com.dirty(false);
console.log(com.config.touched); // true
method
com.isInvalid()
Tells whether the instance is touched and invalid.
com.isInvalid()Returns Boolean The result.
var com = FIND('demo');
com.validate = value => !!value;
com.modify('');
com.validate2();
com.bind('touched');
console.log(com.isInvalid()); // true
method
com.validate2()
Runs the validate delegate and updates the state.
com.validate2()Returns Boolean true for a valid value.
var com = FIND('demo');
com.validate = value => !!value;
console.log(com.validate2()); // false
method
com.notmodified([fields])
Tells whether the value is the same as at the last check.
com.notmodified([fields])| Parameter | Type | Description |
|---|---|---|
fields? |
String|Array |
The fields to compare. |
Returns Boolean true when the value has not changed.
The first check returns false.
var com = FIND('demo');
com.set('Peter');
console.log(com.notmodified()); // false
console.log(com.notmodified()); // true
method
com.parser(value, [prepend])
Adds a parser, or parses a value.
com.parser(value, [prepend])| Parameter | Type | Description |
|---|---|---|
value |
Function|Object |
A parser or the value to parse. |
prepend? |
Boolean |
true adds the parser to the beginning. |
Returns Object The parsed value, or the instance when a parser is added.
A function is added to the parsers of the instance. It receives path, value, type and format, and returns the new value. Any other argument is parsed and returned.
var com = FIND('demo');
com.parser((path, value) => value.toUpperCase());
console.log(com.parser('peter')); // PETER
method
com.formatter(value, [prepend])
Adds a formatter, or formats a value.
com.formatter(value, [prepend])| Parameter | Type | Description |
|---|---|---|
value |
Function|Object |
A formatter or the value to format. |
prepend? |
Boolean |
true adds the formatter to the beginning. |
Returns Object The formatted value, or the instance when a formatter is added.
A function is added to the formatters of the instance. It receives path, value, type and format, and returns the new value. Any other argument is formatted and returned.
var com = FIND('demo');
com.formatter((path, value) => value + '!');
console.log(com.formatter('Peter')); // Peter!
method
com.parsesource(value)
Converts a text list to an array of items.
com.parsesource(value)| Parameter | Type | Description |
|---|---|---|
value |
String |
The list. |
Returns Array Objects with id, name and icon.
Items are separated by a comma. Each item has the form id|name|icon. When the instance has the number type, the identifiers are converted to numbers.
var com = FIND('demo');
console.log(com.parsesource('1|One,2|Two|home')[1]); // {"id":"2","name":"Two","icon":"home"}
method
com.reconfigure(value, [callback], [init], [noemit])
Changes the configuration.
com.reconfigure(value, [callback], [init], [noemit])| Parameter | Type | Description |
|---|---|---|
value |
String|Object |
The configuration. |
callback? |
Function |
Receives key, value, init and prev instead of the configure delegate. |
init? |
Boolean |
The init argument of the delegate. |
noemit? |
Boolean |
true skips the delegate. |
Returns Component The instance.
The value can be a string of key:value pairs, an object, or =path to read an object from a path. The configure delegate receives each key.
var com = FIND('demo');
com.reconfigure('title:Orders;limit:10');
console.log(com.config.title, com.config.limit); // Orders 10
method
com.configdisplay(key, value)
Resolves a value that depends on the display.
com.configdisplay(key, value)| Parameter | Type | Description |
|---|---|---|
key |
String |
The name of the key. |
value |
Object |
The value. |
Returns Object The resolved value.
The value has the form @xs=1,@md=2. Other values are returned unchanged. See Responsive configuration.
var com = FIND('demo');
console.log(com.configdisplay('size', '@xs=1,@lg=4'), WIDTH()); // 1 xs
method
com.data(key, [value])
Reads or writes data of the instance.
com.data(key, [value])| Parameter | Type | Description |
|---|---|---|
key |
String |
The name of the key. |
value? |
Object |
The value to write. |
Returns Object The value.
Nested elements read the keys with the @key path. An empty key is the current value of the instance.
var com = FIND('demo');
com.data('title', 'Orders');
console.log(com.data('title')); // Orders
method
com.datasource(path, callback, [init])
Watches a path that holds a data source.
com.datasource(path, callback, [init])| Parameter | Type | Description |
|---|---|---|
path |
String |
The path of the data source. |
callback |
Function |
The function that receives the data. |
init? |
Boolean |
false skips the initial call. Default: true |
Returns Component The instance.
The callback receives path, value and type. Unless init is false, it also runs after the configuration is loaded. A new call replaces the previous data source.
var com = FIND('demo');
com.datasource('common.items', function(path, value) {
console.log(value); // [1,2]
}, false);
SET('common.items', [1, 2]);
method
com.watch([path], callback, [init])
Watches a path.
com.watch([path], callback, [init])| Parameter | Type | Description |
|---|---|---|
path? |
String |
The path to watch. |
callback |
Function |
The function that receives the changes. |
init? |
Boolean |
true calls the callback immediately. |
Returns Component The instance.
The callback receives path, value and type, and this is the instance. Without a path, the method watches the path of the instance. The watcher is removed together with the instance.
var com = FIND('demo');
com.watch('form.age', function(path, value) {
console.log(path, value); // form.age 30
});
SET('form.age', 30);
method
com.unwatch(path)
Removes the watchers of a path.
com.unwatch(path)| Parameter | Type | Description |
|---|---|---|
path |
String |
The watched path. |
Returns Component The instance.
The method removes all watchers that this instance has on the path.
var com = FIND('demo');
com.watch('form.age', () => console.log('called'));
com.unwatch('form.age');
SET('form.age', 30);
console.log('end'); // end
method
com.on(name, callback)
Listens for a global event.
com.on(name, callback)| Parameter | Type | Description |
|---|---|---|
name |
String |
The name of the event. |
callback |
Function |
The listener. |
Returns Component The instance.
The listener is removed together with the instance. A ^ prefix of the name puts the listener first.
var com = FIND('demo');
com.on('saved', value => console.log(value)); // 5
EMIT('saved', 5);
method
com.off(name, [callback])
Removes a listener of a global event.
com.off(name, [callback])| Parameter | Type | Description |
|---|---|---|
name |
String |
The name of the event. |
callback? |
Function |
The listener. |
Returns Component The instance.
Without a callback, all listeners of the event in this instance are removed.
var com = FIND('demo');
var fn = () => console.log('called');
com.on('saved', fn);
com.off('saved', fn);
console.log(EMIT('saved')); // false
method
com.emit(name, [...args])
Emits a global event.
com.emit(name, [...args])| Parameter | Type | Description |
|---|---|---|
name |
String |
The name of the event. |
args? |
Object |
The arguments of the event. |
Returns Component The instance.
The method calls EMIT().
var com = FIND('demo');
ON('saved', value => console.log(value)); // 5
com.emit('saved', 5);
method
com.command(name, callback)
Registers a command of the instance.
com.command(name, callback)| Parameter | Type | Description |
|---|---|---|
name |
String |
The name of the command. |
callback |
Function |
The handler. |
Returns Component The instance.
A ^ prefix of the name puts the handler first. Commands are called by CMD() or by the global CMD().
var com = FIND('demo');
com.command('clear', () => console.log('cleared')); // cleared
CMD('clear');
method
com.CMD(name, [a], [b], [c], [d], [e])
Calls a command of this instance.
com.CMD(name, [a], [b], [c], [d], [e])| Parameter | Type | Description |
|---|---|---|
name |
String |
The name of the command. |
Returns Component The instance.
The method accepts up to five arguments.
var com = FIND('demo');
com.command('add', (a, b) => console.log(a + b)); // 3
com.CMD('add', 1, 2);
method
com.exec(name, [a], [b], [c], [d], [e])
Calls a method of all nested components.
com.exec(name, [a], [b], [c], [d], [e])| Parameter | Type | Description |
|---|---|---|
name |
String |
The name of the method. |
Returns Component The instance.
Each nested component receives this instance in caller.
var com = FIND('demo');
com.exec('ping', 5);
method
com.GET(path)
Reads a path relative to the scope of the instance.
com.GET(path)| Parameter | Type | Description |
|---|---|---|
path |
String |
The path. |
Returns Object The value.
The ? character is replaced with the path of the scope.
var com = FIND('demo');
SET('users.name', 'Peter');
console.log(com.GET('?.name')); // Peter
method
com.SET(path, value)
Writes a path relative to the scope of the instance.
com.SET(path, value)| Parameter | Type | Description |
|---|---|---|
path |
String |
The path. |
value |
Object |
The value. |
The ? character is replaced with the path of the scope.
var com = FIND('demo');
com.SET('?.age', 30);
console.log(GET('users.age')); // 30
method
com.EXEC(path, [a], [b], [c], [d])
Calls a function by its path.
com.EXEC(path, [a], [b], [c], [d])| Parameter | Type | Description |
|---|---|---|
path |
String |
The path of the function. |
Returns Component The instance.
The ? character is replaced with the path of the scope. A path with the @ prefix calls a method of the parent component.
var com = FIND('child');
com.EXEC('@ping', 5);
method
com.SEEX(path, [a], [b], [c], [d])
Calls a function or writes a value.
com.SEEX(path, [a], [b], [c], [d])| Parameter | Type | Description |
|---|---|---|
path |
String |
The path. |
Returns Component The instance.
The method calls the function on the path. When the path holds no function, the first argument is written to it. The @ prefix works with the parent component.
var com = FIND('child');
com.SEEX('@title', 'Orders');
console.log(FIND('demo').title); // Orders
method
com.makepath(path)
Resolves a path in the scope of the instance.
com.makepath(path)| Parameter | Type | Description |
|---|---|---|
path |
String |
The path. |
Returns String The resolved path.
var com = FIND('demo');
console.log(com.makepath('?.age')); // users.age
method
com.scopepath(path)
Replaces ? in a path with the path of the scope.
com.scopepath(path)| Parameter | Type | Description |
|---|---|---|
path |
String |
The path. |
Returns String The resolved path.
A path without ? is returned unchanged.
var com = FIND('demo');
console.log(com.scopepath('?.age'), com.scopepath('form.age')); // users.age form.age
method
com.setPath(path)
Changes the bound path.
com.setPath(path)| Parameter | Type | Description |
|---|---|---|
path |
String |
The new path. |
Returns Component The instance.
var com = FIND('demo');
com.setPath('form.other');
SET('form.other', 5);
console.log(com.path, com.get()); // form.other 5
method
com.blind()
Disconnects the instance from its path.
com.blind()Returns Component The instance.
var com = FIND('demo');
com.blind();
SET('form.name', 'Peter');
console.log(com.path, com.text() || 'empty'); // null empty
method
self.readonly()
Removes the setter, getter and validate delegates.
self.readonly()Returns Component The instance.
Use it in components that only display content.
var com = FIND('demo');
console.log(com.setter, com.getter); // null null
method
self.singleton()
Allows only one instance of the component in the page.
self.singleton()Returns Component The instance.
Other elements of the same component are removed.
console.log(FIND('demo', true).length, $('ui-component').length); // 1 1
method
self.bindchanges([enable])
Skips the setter when the value has not changed.
self.bindchanges([enable])| Parameter | Type | Description |
|---|---|---|
enable? |
Boolean |
false turns the feature off. Default: true |
Returns Component The instance.
SET('form.name', 'A');
SET('form.name', 'A');
setTimeout(() => console.log(window.calls), 500); // 2
method
self.bindexact([enable])
Ignores changes of nested paths.
self.bindexact([enable])| Parameter | Type | Description |
|---|---|---|
enable? |
Boolean |
false turns the feature off. Default: true |
Returns Component The instance.
The setter runs only for the exact path or its parents.
SET('form.user.name', 'Peter');
setTimeout(() => console.log(window.calls), 500); // 1
method
self.bindvisible([timeout])
Delays the setter until the element is visible.
self.bindvisible([timeout])| Parameter | Type | Description |
|---|---|---|
timeout? |
Number|Boolean |
The delay in milliseconds after the element becomes visible. false turns the feature off. Default: DEF.delaybinder |
Returns Component The instance.
SET('form.name', 'Peter');
setTimeout(() => $('#tab').show(), 500);
setTimeout(() => console.log($('ui-component').text()), 1500); // Peter
method
self.autofill([enable])
Reads values filled in by the browser.
self.autofill([enable])| Parameter | Type | Description |
|---|---|---|
enable? |
Boolean |
false turns the feature off. Default: true |
Returns Component The instance.
After the page is ready, the value of nested fields with the data-jc-bind attribute is written to the path.
$('input').val('Peter');
setTimeout(() => console.log(GET('form.name')), 1400); // Peter
method
com.caniuse(name)
Tells whether a component is available.
com.caniuse(name)| Parameter | Type | Description |
|---|---|---|
name |
String |
The name of the component. |
Returns Number 1 for a declared component, 2 for a lazy one, undefined for an unknown one.
var com = FIND('demo');
console.log(com.caniuse('demo')); // 1
method
com.main()
Returns the closest parent component.
com.main()Returns Component The parent component or null.
var com = FIND('child');
console.log(com.main().name); // demo
method
com.compile([container])
Compiles new content of the element.
com.compile([container])| Parameter | Type | Description |
|---|---|---|
container? |
jQuery|Element |
The element to compile. Default: the component element |
Returns Component The instance.
Call it after you add elements with bindings or components.
var com = FIND('demo');
com.append('<div data-bind="form.name__text"></div>');
com.compile();
SET('form.name', 'Peter');
setTimeout(() => console.log(com.find('div').text()), 500); // Peter
method
com.import(url, [callback], [insert], [preparator])
Imports content into the element.
com.import(url, [callback], [insert], [preparator])| Parameter | Type | Description |
|---|---|---|
url |
String |
The URL. |
callback? |
Function |
Runs after the import. |
insert? |
Boolean |
The insert argument of IMPORT(). |
preparator? |
Function |
Changes the response before the insertion. |
Returns Component The instance.
The method calls IMPORT() with the element as the target.
var com = FIND('demo');
com.import('/part.html', () => console.log('imported')); // imported
method
com.replace(element, [remove])
Moves the instance to another element.
com.replace(element, [remove])| Parameter | Type | Description |
|---|---|---|
element |
jQuery|Element |
The new element. |
remove? |
Boolean |
true removes the original element. |
Returns Component The instance.
var com = FIND('demo');
com.replace($('#target'), true);
console.log(com.dom.id, $('ui-component').length); // target 0
method
com.remove([noclear])
Removes the instance and its element.
com.remove([noclear])| Parameter | Type | Description |
|---|---|---|
noclear? |
Boolean |
true does not start the cleanup immediately. |
Returns Boolean Always true.
The destroy delegate runs during the cleanup. The method is also available as kill().
var com = FIND('demo');
console.log(com.remove()); // true
method
com.find(selector)
Finds nested elements.
com.find(selector)| Parameter | Type | Description |
|---|---|---|
selector |
String|Object |
A selector or an object of selectors. |
Returns jQuery|Object The result.
An object of selectors returns an object of jQuery results with the same keys.
var com = FIND('demo');
console.log(com.find('.label').text()); // Text
method
com.closest(selector)
Finds the closest matching parent.
com.closest(selector)| Parameter | Type | Description |
|---|---|---|
selector |
String |
The selector. |
Returns jQuery The result.
var com = FIND('demo');
console.log(com.closest('body').length); // 1
method
com.parent([selector])
Returns a parent element.
com.parent([selector])| Parameter | Type | Description |
|---|---|---|
selector? |
String |
The selector. |
Returns jQuery The result.
The argument can be:
| Value | Result |
|---|---|
| omitted | The direct parent. |
| A selector | The closest matching element. |
auto | The closest parent with a defined height, otherwise window. |
window | window as a jQuery object. |
document | The document object. |
parent | The direct parent. |
parent2 | The element two levels above the direct parent. |
var com = FIND('demo');
console.log(com.parent().prop('tagName')); // BODY
method
com.html([value])
Reads or replaces the HTML content.
com.html([value])| Parameter | Type | Description |
|---|---|---|
value? |
String|Array |
The new content. |
Returns String|jQuery The content, or the element when value is passed.
An array is joined.
var com = FIND('demo');
com.html('<b>A</b>');
console.log(com.html()); // <b>A</b>
method
com.text([value])
Reads or replaces the text content.
com.text([value])| Parameter | Type | Description |
|---|---|---|
value? |
String|Array |
The new content. |
Returns String|jQuery The content, or the element when value is passed.
An array is joined.
var com = FIND('demo');
com.text('<b>A</b>');
console.log(com.html()); // <b>A</b>
method
com.append(value)
Adds content to the end of the element.
com.append(value)| Parameter | Type | Description |
|---|---|---|
value |
String|Array |
The content. |
Returns jQuery The element.
An array is joined.
var com = FIND('demo');
com.empty();
com.append('<b>A</b>');
console.log(com.html()); // <b>A</b>
method
com.empty()
Removes the content of the element.
com.empty()Returns jQuery The element.
Nested components are removed too.
var com = FIND('demo');
com.empty();
console.log(com.html() === ''); // true
method
com.event(name, [selector], callback)
Attaches a jQuery event to the element.
com.event(name, [selector], callback)| Parameter | Type | Description |
|---|---|---|
name |
String |
The name of the event. |
selector? |
String |
The delegated selector. |
callback |
Function |
The handler. |
Returns Component The instance.
The arguments are the same as in the jQuery on() method.
var com = FIND('demo');
com.event('click', '.label', () => console.log('clicked')); // clicked
com.find('.label').click();
method
com.aclass(classes, [timeout])
Adds CSS classes.
com.aclass(classes, [timeout])| Parameter | Type | Description |
|---|---|---|
classes |
String |
The classes. |
timeout? |
Number |
The delay in milliseconds. |
Returns Component The instance.
var com = FIND('demo');
com.aclass('active');
console.log(com.hclass('active')); // true
method
com.rclass([classes], [timeout])
Removes CSS classes.
com.rclass([classes], [timeout])| Parameter | Type | Description |
|---|---|---|
classes? |
String |
The classes. |
timeout? |
Number |
The delay in milliseconds. |
Returns Component The instance.
Without an argument, all classes are removed.
var com = FIND('demo');
com.aclass('active');
com.rclass('active');
console.log(com.hclass('active')); // false
method
com.rclass2(search)
Removes CSS classes that contain a text.
com.rclass2(search)| Parameter | Type | Description |
|---|---|---|
search |
String|RegExp |
The text or a regular expression. |
Returns Component The instance.
var com = FIND('demo');
com.aclass('col-1 col-2');
com.rclass2('col-');
console.log(com.hclass('col-1')); // false
method
com.tclass(classes, [state])
Toggles CSS classes.
com.tclass(classes, [state])| Parameter | Type | Description |
|---|---|---|
classes |
String |
The classes. |
state? |
Boolean |
Forces the state. |
Returns Component The instance.
var com = FIND('demo');
com.tclass('active', true);
console.log(com.hclass('active')); // true
method
com.hclass(name)
Tells whether the element has a CSS class.
com.hclass(name)| Parameter | Type | Description |
|---|---|---|
name |
String |
The class. |
Returns Boolean The result.
var com = FIND('demo');
console.log(com.hclass('missing')); // false
method
com.attr(name, [value])
Reads or writes an attribute.
com.attr(name, [value])| Parameter | Type | Description |
|---|---|---|
name |
String |
The attribute. |
value? |
String |
The new value. |
Returns String|Component The value, or the instance when value is passed.
var com = FIND('demo');
com.attr('title', 'Orders');
console.log(com.attr('title')); // Orders
method
com.attrd(name, [value])
Reads or writes a data- attribute.
com.attrd(name, [value])| Parameter | Type | Description |
|---|---|---|
name |
String |
The name without the data- prefix. |
value? |
String |
The new value. |
Returns String|Component The value, or the instance when value is passed.
var com = FIND('demo');
com.attrd('id', '5');
console.log(com.attr('data-id')); // 5
method
com.attrd2(name)
Reads a data- attribute from the element or its closest parent that has it.
com.attrd2(name)| Parameter | Type | Description |
|---|---|---|
name |
String |
The name without the data- prefix. |
Returns String The value.
var com = FIND('demo');
console.log(com.attrd2('group')); // main
method
com.css(name, [value])
Reads or writes a CSS property.
com.css(name, [value])| Parameter | Type | Description |
|---|---|---|
name |
String |
The property. |
value? |
String |
The new value. |
Returns String|Component The value, or the instance when value is passed.
var com = FIND('demo');
com.css('display', 'block');
console.log(com.css('display')); // block
method
com.visible([callback])
Tells whether the element is visible.
com.visible([callback])| Parameter | Type | Description |
|---|---|---|
callback? |
Function |
Runs when the element is visible. |
Returns Boolean The current visibility.
With a callback, the function runs as soon as the element is visible.
var com = FIND('demo');
console.log(com.visible()); // true
method
com.width([callback])
Returns the width of the element in pixels.
com.width([callback])| Parameter | Type | Description |
|---|---|---|
callback? |
Function |
Receives the width. |
Returns Number The width.
With a callback, the function runs as soon as the width is greater than zero.
var com = FIND('demo');
com.css('display', 'block');
com.css('width', '200px');
console.log(com.width()); // 200
method
com.height([callback])
Returns the height of the element in pixels.
com.height([callback])| Parameter | Type | Description |
|---|---|---|
callback? |
Function |
Receives the height. |
Returns Number The height.
With a callback, the function runs as soon as the height is greater than zero.
var com = FIND('demo');
com.css('display', 'block');
com.css('height', '50px');
console.log(com.height()); // 50
method
com.autofocus([selector])
Moves the focus to the first visible field.
com.autofocus([selector])| Parameter | Type | Description |
|---|---|---|
selector? |
String |
The fields. Default: input,select,textarea |
Returns Component The instance.
The method tries again for a short time when no field can receive the focus. It does nothing on mobile devices.
var com = FIND('demo');
com.autofocus();
console.log(document.activeElement.tagName); // INPUT
method
com.faicon(value)
Completes the name of an icon.
com.faicon(value)| Parameter | Type | Description |
|---|---|---|
value |
String |
The name of the icon. |
Returns String The CSS classes.
A name without a space receives DEF.iconprefix, which is ti ti-. The method is also available as icon().
var com = FIND('demo');
console.log(com.faicon('home')); // ti ti-home