Spanish Total.js
English
Documentation For AI agents
Contents

Plugin

The properties, the methods and the delegates of a plugin instance.

.md GitHub
On this page

A plugin instance is the first argument of the function given to PLUGIN(), usually named exports. It is created for each ui-plugin element. The methods work with paths relative to the plugin, as explained in the Plugins guide. The examples below use a plugin with the path orders.

API

property

name

The path of the plugin.

Returns String The path of the plugin.

property

path

The path of the plugin model.

Returns Path The path of the plugin model.

See Path.

property

config

The configuration of the plugin.

Returns Object The configuration of the plugin.

property

element

The element of the plugin as a jQuery object.

Returns jQuery The element of the plugin as a jQuery object.

property

dom

The element of the plugin.

Returns HTMLElement The element of the plugin.

property

scope

The object that paths are resolved against, which is window.

Returns Object The object that paths are resolved against, which is window.

property

cls

The value of the path attribute of the element.

Returns String The value of the path attribute of the element.

property

ready

Is true when the plugin is initialized.

Returns Boolean Is true when the plugin is initialized.

property

plugin

The parent plugin of a plugin nested with a ? in its path, otherwise null.

Returns Plugin The parent plugin of a plugin nested with a ? in its path, otherwise null.

property

parent

The same value as the plugin property.

Returns Plugin The same value as the plugin property.

A nested plugin
<ui-plugin path="orders">
    <ui-plugin path="?.detail"></ui-plugin>
</ui-plugin>

<script>
PLUGIN('orders', function(exports) {});

PLUGIN('orders.detail', function(exports) {
    exports.make = () => console.log(exports.name, exports.parent.name);
});
// logs: orders.detail orders
</script>
property

caller

The plugin that called a method of this plugin through EXEC().

Returns Plugin The plugin that called a method of this plugin through EXEC().

See talker().

property

model

Reads or replaces the model of the plugin.

Returns Object Reads or replaces the model of the plugin.

Assigning a value does not notify anything bound to the path. Use set() or upd() for that.

Read the model
plugin.set('count', 2);
console.log(JSON.stringify(plugin.model));
// logs: {"count":2}
plugin.model = { count: 3 };
console.log(orders.count);
// logs: 3
property

modified

Returns the model and clears the state of its components.

Returns Object The model of the plugin.

Reading the property returns the whole model, like get(), and clears the touched and modified state of the components in the plugin, like the flag @reset. Read it after saving a form.

Save a form
<ui-plugin path="orders">
    <ui-component name="field" path="?.city"></ui-component>
</ui-plugin>

<script>
COMPONENT('field', function(self) {});

PLUGIN('orders', function(exports) {
    exports.save = function() {
        console.log(JSON.stringify(exports.modified));
        console.log(FIND('field').config.modified);
    };
});

ON('ready', function() {
    SET('orders.city @change', 'Prague');
    EXEC('orders/save');
});
// logs: {"city":"Prague"}
// logs: 0
</script>
property

data

The same value as the model property.

Returns Object The same value as the model property.

property

form

Returns the model and clears the touched and modified state of the components in the path of the plugin.

Returns Object Returns the model and clears the touched and modified state of the components in the path of the plugin.

Read a form and reset its state
<ui-plugin path="orders">
    <ui-component name="field" path="?.name"></ui-component>
</ui-plugin>

<script>
PLUGIN('orders', function(exports) {
    window.plugin = exports;
});

COMPONENT('field', function(self) {
    window.com = self;
});

com.modify('Anna');
console.log(com.element.attr('class'));
// logs: ui-touched ui-modified
console.log(plugin.form.name, com.element.hclass('ui-touched'));
// logs: Anna false
</script>
delegate

make()

Runs when the plugin is created.
Initialize the plugin
<ui-plugin path="orders"></ui-plugin>

<script>
PLUGIN('orders', function(exports) {
    exports.make = () => console.log(exports.path.toString());
});
// logs: orders
</script>
delegate

destroy()

Runs when the plugin is removed.

The regular cleanup removes the plugins whose element left the page.

Release resources
<ui-plugin path="orders"></ui-plugin>

<script>
PLUGIN('orders', function(exports) {
    exports.destroy = () => console.log('destroyed');
});

$('ui-plugin').remove();
jComponent.free();
// logs: destroyed
</script>
method

get([path])

Reads a value from the model.
ParameterTypeDescription
path? String A path relative to the plugin.

Returns Object The value.

Without a path, the method returns the whole model. A path with the @reset flag also clears the state of the components in the path.

Read a value
plugin.set('user.name', 'Anna');
console.log(plugin.get('user.name'));
// logs: Anna
method

set([path], value)

Writes a value to the model and notifies everything bound to the path.
ParameterTypeDescription
path String A path relative to the plugin.
value Object The value to write.

With an object as the only argument, the method replaces the whole model.

Write a value
plugin.set('status', 'open');
console.log(orders.status);
// logs: open
Replace the model
plugin.set({ status: 'new', items: [] });
console.log(JSON.stringify(orders));
// logs: {"status":"new","items":[]}
method

push(path, value)

Adds a value to an array in the model.
ParameterTypeDescription
path String A path relative to the plugin.
value Object The value to write.

Each item of an array value is added separately.

Add items
plugin.push('items', 1);
plugin.push('items', [2, 3]);
console.log(orders.items.join());
// logs: 1,2,3
method

inc(path, [value])

Increases a number in the model.
ParameterTypeDescription
path String A path relative to the plugin.
value? Number The increment. Default: 1
Count
plugin.inc('count');
plugin.inc('count', 4);
console.log(orders.count);
// logs: 5
method

toggle(path)

Negates a value in the model.
ParameterTypeDescription
path String A path relative to the plugin.
Switch a value
plugin.toggle('open');
console.log(orders.open);
// logs: true
method

nul(path)

Writes null to the model.
ParameterTypeDescription
path String A path relative to the plugin.
Clear a value
plugin.set('user', 'Anna');
plugin.nul('user');
console.log(orders.user);
// logs: null
method

upd(path)

Notifies everything bound to a path, without changing the value.
ParameterTypeDescription
path String A path relative to the plugin.

The method is also available as update().

Announce a direct change
plugin.watch('count', value => console.log(value));
orders.count = 5;
plugin.upd('count');
// logs: 5
method

watch(path, callback, [autoinit])

Calls a function on each change at a path of the model.
ParameterTypeDescription
path String A path relative to the plugin.
callback Function A function (value, flags, path).
autoinit? Boolean Calls the function right away with the current value. Default: false

With a function as the first argument, the whole model is observed. Join several paths with +.

Observe a value
plugin.watch('status', (value, flags, path) => console.log(value, path));
plugin.set('status', 'open');
// logs: open orders.status
method

unwatch(path, [callback])

Removes watchers registered with watch().
ParameterTypeDescription
path String A path relative to the plugin.
callback? Function The watcher to remove.

Without callback, all the watchers of the path are removed. With a function as the only argument, the watcher of the whole model is removed. Join several paths with +.

Stop observing a value
var log = value => console.log('status', value);

plugin.watch('status', log);
plugin.set('status', 'open');
plugin.unwatch('status', log);
plugin.set('status', 'closed');
// logs: status open
method

makepath(path)

Replaces the ? in a path with the path of the plugin.
ParameterTypeDescription
path String The path.

Returns String The resulting path.

Build a path
console.log(plugin.makepath('?.items'));
// logs: orders.items
method

cachepath(path, expire, [def])

Keeps a value of the model in the cache between visits.
ParameterTypeDescription
path String A path relative to the plugin.
expire String The lifetime of the saved value, for example 1 day.
def? Object|Function The value used when nothing is saved.

The same as the global CACHEPATH(), with a path relative to the plugin.

Remember a filter
plugin.cachepath('filter', '1 day', 'all');
console.log(orders.filter);
// logs: all
method

format(value)

Inserts the path of the plugin into a text.
ParameterTypeDescription
value String The text.

Returns String The resulting text.

The text {0} is replaced with the path. Without {0}, the path is added to the end of the text.

Insert the path
console.log(plugin.format('{0}.items'));
// logs: orders.items
method

exec(name, [a], [b], [c], [d], [e])

Calls a method of this plugin.
ParameterTypeDescription
name String The method 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.

The name can carry flags. The method runs without this, so refer to the plugin with exports.

Call a method
plugin.exec('save', 1);
// logs: save 1
method

cmd(name, [a], [b], [c], [d])

Runs a command on the components inside the plugin element.
ParameterTypeDescription
name String The command, optionally followed by flags.
a? Object The first argument.
b? Object The second argument.
c? Object The third argument.
d? Object The fourth argument.

Components register commands with command(). CMD() is the same method. To reach the components of the whole page, use the global CMD().

Clear the lists of a plugin
<ui-plugin path="orders">
    <ui-component name="list"></ui-component>
</ui-plugin>
<ui-component name="list"></ui-component>

<script>
COMPONENT('list', function(self) {
    self.command('clear', () => console.log('clear'));
});

PLUGIN('orders', function(exports) {});

ON('ready', () => PLUGINS.orders.cmd('clear'));
// logs: clear
</script>
method

setter(name, [a], [b], [c], [d])

Calls a method of the components inside the plugin, in the form component/method.
ParameterTypeDescription
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.

The method is also available as SETTER().

Call components inside the plugin
<ui-plugin path="orders">
    <ui-component name="grid"></ui-component>
</ui-plugin>

<script>
PLUGIN('orders', function(exports) {
    window.plugin = exports;
});

COMPONENT('grid', function(self) {
    self.reload = value => console.log('reload', value);
});

plugin.setter('grid/reload', 1);
// logs: reload 1
</script>
method

alias(name)

Registers the plugin under another name in PLUGINS.
ParameterTypeDescription
name String The other name.
Use a short name
plugin.alias('o');
console.log(PLUGINS.o === plugin);
// logs: true
method

talker()

Marks this plugin as the caller of the next EXEC() call.

The called plugin finds it in its caller property.

Find out who called
<ui-plugin path="orders"></ui-plugin>
<ui-plugin path="users"></ui-plugin>

<script>
PLUGIN('orders', function(exports) {
    window.plugin = exports;
});

PLUGIN('users', function(exports) {
    exports.load = () => console.log(exports.caller.name);
});

plugin.talker();
EXEC('users/load');
// logs: orders
</script>
method

on(name, callback)

Registers a handler of a global event.
ParameterTypeDescription
name String The event name.
callback Function The handler.

Join several event names with +. See Events.

Handle an event
var handler = value => console.log(value);
plugin.on('saved', handler);
plugin.emit('saved', 1);
plugin.off('saved', handler);
plugin.emit('saved', 2);
// logs: 1
method

off(name, [callback])

Removes a handler of a global event.
ParameterTypeDescription
name String The event name.
callback? Function The handler to remove.

Without a callback, all handlers of the event registered on this plugin are removed.

method

emit(name, [a], [b], [c], [d])

Emits a global event.
ParameterTypeDescription
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.
method

ajax(url, [data], [callback])

Sends a request.
ParameterTypeDescription
url String The method and the address.
data? Object The data to send.
callback? Function A function (response), or a path.

The address has the form METHOD /url. A string as the callback is a path relative to the plugin that receives the response.

Store the response in the model
plugin.ajax('GET /api/orders/', 'items');
console.log(orders.items.length);
// logs: 2
method

tapi(name, [data], [callback])

Calls an operation of the API at the address in DEF.api.
ParameterTypeDescription
name String The name of the operation.
data? Object The data to send.
callback? Function A function (response), or a path.

A string as the callback is a path relative to the plugin that receives the response.

Call an API operation
DEF.api = '/api/';
plugin.tapi('orders_list', 'items');
// sends: POST /api/ {"schema":"orders_list"}
method

pipe(name, [data], [callback])

Calls an operation of the API at the address in DEF.pipe.
ParameterTypeDescription
name String The name of the operation.
data? Object The data to send.
callback? Function A function (response), or a path.

It works in the same way as tapi().

Call a pipe operation
DEF.pipe = '/pipe/';
plugin.pipe('orders_save', { id: 1 }, response => console.log(response.ok));
// logs: true
Made in Europe