Spanish Total.js
English
Documentation For AI agents
Contents

Component

The delegates, properties and methods of a component instance.

.md GitHub
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.

The delegate runs once, after the declaration, when the element is in the DOM.

Defining make
<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.

Only the first instance of the page calls this delegate.

Defining init
<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.

The delegate runs after the first setter and state calls.

Defining done
<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.
ParameterTypeDescription
hidden Boolean true when the element is hidden.

The delegate runs once and receives true for a hidden element.

Defining released
<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.
ParameterTypeDescription
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.

Defining configure
<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.
ParameterTypeDescription
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:

TypeSource
0The initialization.
1A manual change, such as SET().
2An input of the user.
3A default value or a reset.
Defining setter
<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.
ParameterTypeDescription
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.

Defining getter
<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.

It receives the same arguments. Use it to extend a component and keep its setter.

Defining setter2
<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.

It receives the parsed value and the realtime argument.

Defining getter2
<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.
ParameterTypeDescription
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:

WhatMeaning
1Validation.
2The touched state changed.
3A reset.
4An update.
5A new value.
6A notification.
Defining state
<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.

It receives the same arguments.

Defining state2
<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.
ParameterTypeDescription
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.

Defining validate
<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.
ParameterTypeDescription
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.

Release timers and external listeners here.

Defining destroy
<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.
Reading 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.

The value is empty when the name has no version. The element also receives the jc-v{version} class.

Reading version
<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.

The $id configuration key sets it. Otherwise it has the same value as ID.

Reading 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.
Reading 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.

The value is empty when the component has no path.

Reading 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.

The value is passed to parsers and formatters. Set it in the declaration or with the $type key.

Reading type
<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.

When the property is empty, config.format is used.

Reading format
<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.

The default value is true.

Reading trim
<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.

It also holds the state keys modified, touched, invalid and, when set, disabled.

Reading config
<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.
Reading 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.
Reading 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.

The property is missing when the component has no parent component.

Reading owner
<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().
Reading 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.
Reading 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.

The property exists when the path contains ?.

Reading scope
<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.

The property exists when the path contains ?.

Reading pathscope
<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.

The property exists when the path contains ?.

Reading plugin
<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.
Reading 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.
Reading 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.
ParameterTypeDescription
path? String Another path.

Returns Object The value.

With an argument, the method reads another path.

Using get()
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.
ParameterTypeDescription
value Object The new value.
type? Number The type passed to the setter.

Returns Component The instance.

The instance is marked as modified.

Using set()
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.
ParameterTypeDescription
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.

Using modify()
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.
ParameterTypeDescription
value Number The increment.
type? Number The type passed to the setter.

Returns Component The instance.

Using inc()
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.
ParameterTypeDescription
value Object The fields to merge.
type? Number The type passed to the setter.

Returns Component The instance.

Using extend()
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.
ParameterTypeDescription
value Object The item.
type? Number The type passed to the setter.

Returns Component The instance.

Using push()
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.
ParameterTypeDescription
value Object The new value.
type? Number The type of the change.

Returns Component The instance.

Bound components, binders and watchers are not called.

Using rewrite()
var com = FIND('demo');
com.rewrite('Peter');
console.log(GET('form.name'));  // Peter
method

com.update([type])

Writes the current value again.
ParameterTypeDescription
type? Number The type passed to the setter.

Returns Component The instance.

All listeners of the path are notified.

Using update()
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.
ParameterTypeDescription
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.

Using refresh()
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.

Returns Component The instance.

The method calls NOTIFY() with the path of the instance.

Using notify()
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.

Returns Component The instance.

Using skip()
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.
ParameterTypeDescription
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.

FlagEffect
touched, touchMarks the instance as touched.
modified, modify, changed, changeMarks the instance as modified.
resetClears the touched and modified state.
validateValidates the value.
setterCalls the setter.

When value is passed, it is written to the path, and the setter of this instance runs only with the setter flag.

Using bind()
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.

Returns Component The instance.

The value is validated again.

Using reset()
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.
ParameterTypeDescription
reset? Boolean false keeps the state of the instance. Default: true

Returns Component The instance.

The default value comes from the default attribute.

Using default()
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.
ParameterTypeDescription
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().

Using change()
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.
ParameterTypeDescription
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.

Using dirty()
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.

Returns Boolean The result.

Using isInvalid()
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.

Returns Boolean true for a valid value.

Using validate2()
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.
ParameterTypeDescription
fields? String|Array The fields to compare.

Returns Boolean true when the value has not changed.

The first check returns false.

Using notmodified()
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.
ParameterTypeDescription
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.

Using parser()
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.
ParameterTypeDescription
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.

Using formatter()
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.
ParameterTypeDescription
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.

Using parsesource()
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.
ParameterTypeDescription
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.

Using reconfigure()
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.
ParameterTypeDescription
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.

Using configdisplay()
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.
ParameterTypeDescription
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.

Using data()
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.
ParameterTypeDescription
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.

Using datasource()
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.
ParameterTypeDescription
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.

Using watch()
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.
ParameterTypeDescription
path String The watched path.

Returns Component The instance.

The method removes all watchers that this instance has on the path.

Using unwatch()
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.
ParameterTypeDescription
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.

Using on()
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.
ParameterTypeDescription
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.

Using off()
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.
ParameterTypeDescription
name String The name of the event.
args? Object The arguments of the event.

Returns Component The instance.

The method calls EMIT().

Using 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.
ParameterTypeDescription
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().

Using command()
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.
ParameterTypeDescription
name String The name of the command.

Returns Component The instance.

The method accepts up to five arguments.

Using CMD()
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.
ParameterTypeDescription
name String The name of the method.

Returns Component The instance.

Each nested component receives this instance in caller.

Using exec()
var com = FIND('demo');
com.exec('ping', 5);
method

com.GET(path)

Reads a path relative to the scope of the instance.
ParameterTypeDescription
path String The path.

Returns Object The value.

The ? character is replaced with the path of the scope.

Using GET()
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.
ParameterTypeDescription
path String The path.
value Object The value.

The ? character is replaced with the path of the scope.

Using SET()
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.
ParameterTypeDescription
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.

Using EXEC()
var com = FIND('child');
com.EXEC('@ping', 5);
method

com.SEEX(path, [a], [b], [c], [d])

Calls a function or writes a value.
ParameterTypeDescription
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.

Using SEEX()
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.
ParameterTypeDescription
path String The path.

Returns String The resolved path.

Using makepath()
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.
ParameterTypeDescription
path String The path.

Returns String The resolved path.

A path without ? is returned unchanged.

Using scopepath()
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.
ParameterTypeDescription
path String The new path.

Returns Component The instance.

Using setPath()
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.

Returns Component The instance.

Using blind()
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.

Returns Component The instance.

Use it in components that only display content.

Using readonly()
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.

Returns Component The instance.

Other elements of the same component are removed.

Using singleton()
console.log(FIND('demo', true).length, $('ui-component').length);  // 1 1
method

self.bindchanges([enable])

Skips the setter when the value has not changed.
ParameterTypeDescription
enable? Boolean false turns the feature off. Default: true

Returns Component The instance.

Using bindchanges()
SET('form.name', 'A');
SET('form.name', 'A');
setTimeout(() => console.log(window.calls), 500);  // 2
method

self.bindexact([enable])

Ignores changes of nested paths.
ParameterTypeDescription
enable? Boolean false turns the feature off. Default: true

Returns Component The instance.

The setter runs only for the exact path or its parents.

Using bindexact()
SET('form.user.name', 'Peter');
setTimeout(() => console.log(window.calls), 500);  // 1
method

self.bindvisible([timeout])

Delays the setter until the element is visible.
ParameterTypeDescription
timeout? Number|Boolean The delay in milliseconds after the element becomes visible. false turns the feature off. Default: DEF.delaybinder

Returns Component The instance.

Using bindvisible()
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.
ParameterTypeDescription
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.

Using autofill()
$('input').val('Peter');
setTimeout(() => console.log(GET('form.name')), 1400);  // Peter
method

com.caniuse(name)

Tells whether a component is available.
ParameterTypeDescription
name String The name of the component.

Returns Number 1 for a declared component, 2 for a lazy one, undefined for an unknown one.

Using caniuse()
var com = FIND('demo');
console.log(com.caniuse('demo'));  // 1
method

com.main()

Returns the closest parent component.

Returns Component The parent component or null.

Using main()
var com = FIND('child');
console.log(com.main().name);  // demo
method

com.compile([container])

Compiles new content of the element.
ParameterTypeDescription
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.

Using compile()
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.
ParameterTypeDescription
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.

Using import()
var com = FIND('demo');
com.import('/part.html', () => console.log('imported'));  // imported
method

com.replace(element, [remove])

Moves the instance to another element.
ParameterTypeDescription
element jQuery|Element The new element.
remove? Boolean true removes the original element.

Returns Component The instance.

Using replace()
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.
ParameterTypeDescription
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().

Using remove()
var com = FIND('demo');
console.log(com.remove());  // true
method

com.find(selector)

Finds nested elements.
ParameterTypeDescription
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.

Using find()
var com = FIND('demo');
console.log(com.find('.label').text());  // Text
method

com.closest(selector)

Finds the closest matching parent.
ParameterTypeDescription
selector String The selector.

Returns jQuery The result.

Using closest()
var com = FIND('demo');
console.log(com.closest('body').length);  // 1
method

com.parent([selector])

Returns a parent element.
ParameterTypeDescription
selector? String The selector.

Returns jQuery The result.

The argument can be:

ValueResult
omittedThe direct parent.
A selectorThe closest matching element.
autoThe closest parent with a defined height, otherwise window.
windowwindow as a jQuery object.
documentThe document object.
parentThe direct parent.
parent2The element two levels above the direct parent.
Using parent()
var com = FIND('demo');
console.log(com.parent().prop('tagName'));  // BODY
method

com.html([value])

Reads or replaces the HTML content.
ParameterTypeDescription
value? String|Array The new content.

Returns String|jQuery The content, or the element when value is passed.

An array is joined.

Using html()
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.
ParameterTypeDescription
value? String|Array The new content.

Returns String|jQuery The content, or the element when value is passed.

An array is joined.

Using text()
var com = FIND('demo');
com.text('<b>A</b>');
console.log(com.html());  // &lt;b&gt;A&lt;/b&gt;
method

com.append(value)

Adds content to the end of the element.
ParameterTypeDescription
value String|Array The content.

Returns jQuery The element.

An array is joined.

Using append()
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.

Returns jQuery The element.

Nested components are removed too.

Using empty()
var com = FIND('demo');
com.empty();
console.log(com.html() === '');  // true
method

com.event(name, [selector], callback)

Attaches a jQuery event to the element.
ParameterTypeDescription
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.

Using event()
var com = FIND('demo');
com.event('click', '.label', () => console.log('clicked'));  // clicked
com.find('.label').click();
method

com.aclass(classes, [timeout])

Adds CSS classes.
ParameterTypeDescription
classes String The classes.
timeout? Number The delay in milliseconds.

Returns Component The instance.

Using aclass()
var com = FIND('demo');
com.aclass('active');
console.log(com.hclass('active'));  // true
method

com.rclass([classes], [timeout])

Removes CSS classes.
ParameterTypeDescription
classes? String The classes.
timeout? Number The delay in milliseconds.

Returns Component The instance.

Without an argument, all classes are removed.

Using rclass()
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.
ParameterTypeDescription
search String|RegExp The text or a regular expression.

Returns Component The instance.

Using rclass2()
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.
ParameterTypeDescription
classes String The classes.
state? Boolean Forces the state.

Returns Component The instance.

Using tclass()
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.
ParameterTypeDescription
name String The class.

Returns Boolean The result.

Using hclass()
var com = FIND('demo');
console.log(com.hclass('missing'));  // false
method

com.attr(name, [value])

Reads or writes an attribute.
ParameterTypeDescription
name String The attribute.
value? String The new value.

Returns String|Component The value, or the instance when value is passed.

Using attr()
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.
ParameterTypeDescription
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.

Using attrd()
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.
ParameterTypeDescription
name String The name without the data- prefix.

Returns String The value.

Using attrd2()
var com = FIND('demo');
console.log(com.attrd2('group'));  // main
method

com.css(name, [value])

Reads or writes a CSS property.
ParameterTypeDescription
name String The property.
value? String The new value.

Returns String|Component The value, or the instance when value is passed.

Using css()
var com = FIND('demo');
com.css('display', 'block');
console.log(com.css('display'));  // block
method

com.visible([callback])

Tells whether the element is visible.
ParameterTypeDescription
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.

Using visible()
var com = FIND('demo');
console.log(com.visible());  // true
method

com.width([callback])

Returns the width of the element in pixels.
ParameterTypeDescription
callback? Function Receives the width.

Returns Number The width.

With a callback, the function runs as soon as the width is greater than zero.

Using width()
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.
ParameterTypeDescription
callback? Function Receives the height.

Returns Number The height.

With a callback, the function runs as soon as the height is greater than zero.

Using height()
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.
ParameterTypeDescription
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.

Using autofocus()
var com = FIND('demo');
com.autofocus();
console.log(document.activeElement.tagName);  // INPUT
method

com.faicon(value)

Completes the name of an icon.
ParameterTypeDescription
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().

Using faicon()
var com = FIND('demo');
console.log(com.faicon('home'));  // ti ti-home
Made in Europe