---
title: Plugin
project: jComponent V20
version: 20.015
section: reference
language: en
license: MIT
url: https://totaljs.es/docs/jcomponent-v20/20-015/reference/plugin/
updated: 2026-11-18T13:58:19.462Z
---

# Plugin

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

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

## API

### name

Type: `property`

The path of the plugin.

Returns: `String` The path of the plugin.

### path

Type: `property`

The path of the plugin model.

Returns: `Path` The path of the plugin model.

See [Path](../path/).

### config

Type: `property`

The configuration of the plugin.

Returns: `Object` The configuration of the plugin.

### element

Type: `property`

The element of the plugin as a jQuery object.

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

### dom

Type: `property`

The element of the plugin.

Returns: `HTMLElement` The element of the plugin.

### scope

Type: `property`

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

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

### cls

Type: `property`

The value of the `path` attribute of the element.

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

### ready

Type: `property`

Is `true` when the plugin is initialized.

Returns: `Boolean` Is `true` when the plugin is initialized.

### plugin

Type: `property`

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

### parent

Type: `property`

The same value as the `plugin` property.

Returns: `Plugin` The same value as the `plugin` property.

Example: A nested plugin
```html
<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>
```

### caller

Type: `property`

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()`.

### model

Type: `property`

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.

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

### modified

Type: `property`

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.

Example: Save a form
```html
<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>
```

### data

Type: `property`

The same value as the `model` property.

Returns: `Object` The same value as the `model` property.

### form

Type: `property`

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.

Example: Read a form and reset its state
```html
<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>
```

### make()

Type: `delegate`

Runs when the plugin is created.

Example: Initialize the plugin
```html
<ui-plugin path="orders"></ui-plugin>

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

### destroy()

Type: `delegate`

Runs when the plugin is removed.

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

Example: Release resources
```html
<ui-plugin path="orders"></ui-plugin>

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

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

### get([path])

Type: `method`

Reads a value from the model.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `path` | `String` | no |  | 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.

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

### set([path], value)

Type: `method`

Writes a value to the model and notifies everything bound to the path.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `path` | `String` | yes |  | A path relative to the plugin. |
| `value` | `Object` | yes |  | The value to write. |

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

Example: Write a value
```js
plugin.set('status', 'open');
console.log(orders.status);
// logs: open
```

Example: Replace the model
```js
plugin.set({ status: 'new', items: [] });
console.log(JSON.stringify(orders));
// logs: {"status":"new","items":[]}
```

### push(path, value)

Type: `method`

Adds a value to an array in the model.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `path` | `String` | yes |  | A path relative to the plugin. |
| `value` | `Object` | yes |  | The value to write. |

Each item of an array value is added separately.

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

### inc(path, [value])

Type: `method`

Increases a number in the model.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `path` | `String` | yes |  | A path relative to the plugin. |
| `value` | `Number` | no | `1` | The increment. |

Example: Count
```js
plugin.inc('count');
plugin.inc('count', 4);
console.log(orders.count);
// logs: 5
```

### toggle(path)

Type: `method`

Negates a value in the model.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `path` | `String` | yes |  | A path relative to the plugin. |

Example: Switch a value
```js
plugin.toggle('open');
console.log(orders.open);
// logs: true
```

### nul(path)

Type: `method`

Writes `null` to the model.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `path` | `String` | yes |  | A path relative to the plugin. |

Example: Clear a value
```js
plugin.set('user', 'Anna');
plugin.nul('user');
console.log(orders.user);
// logs: null
```

### upd(path)

Type: `method`

Notifies everything bound to a path, without changing the value.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `path` | `String` | yes |  | A path relative to the plugin. |

The method is also available as `update()`.

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

### watch(path, callback, [autoinit])

Type: `method`

Calls a function on each change at a path of the model.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `path` | `String` | yes |  | A path relative to the plugin. |
| `callback` | `Function` | yes |  | A function `(value, flags, path)`. |
| `autoinit` | `Boolean` | no | `false` | Calls the function right away with the current value. |

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

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

### unwatch(path, [callback])

Type: `method`

Removes watchers registered with `watch()`.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `path` | `String` | yes |  | A path relative to the plugin. |
| `callback` | `Function` | no |  | 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 `+`.

Example: Stop observing a value
```js
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
```

### makepath(path)

Type: `method`

Replaces the `?` in a path with the path of the plugin.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `path` | `String` | yes |  | The path. |

Returns: `String` The resulting path.

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

### cachepath(path, expire, [def])

Type: `method`

Keeps a value of the model in the cache between visits.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `path` | `String` | yes |  | A path relative to the plugin. |
| `expire` | `String` | yes |  | The lifetime of the saved value, for example `1 day`. |
| `def` | `Object|Function` | no |  | The value used when nothing is saved. |

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

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

### format(value)

Type: `method`

Inserts the path of the plugin into a text.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `value` | `String` | yes |  | 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.

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

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

Type: `method`

Calls a method of this plugin.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | The method name. |
| `a` | `Object` | no |  | The first argument. |
| `b` | `Object` | no |  | The second argument. |
| `c` | `Object` | no |  | The third argument. |
| `d` | `Object` | no |  | The fourth argument. |
| `e` | `Object` | no |  | The fifth argument. |

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

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

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

Type: `method`

Runs a command on the components inside the plugin element.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | The command, optionally followed by flags. |
| `a` | `Object` | no |  | The first argument. |
| `b` | `Object` | no |  | The second argument. |
| `c` | `Object` | no |  | The third argument. |
| `d` | `Object` | no |  | The fourth argument. |

Components register commands with [command()](../component/). `CMD()` is the same method. To reach the components of the whole page, use the global [CMD()](../globals/).

Example: Clear the lists of a plugin
```html
<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>
```

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

Type: `method`

Calls a method of the components inside the plugin, in the form `component/method`.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | The target, for example `grid/refresh`. |
| `a` | `Object` | no |  | The first argument. |
| `b` | `Object` | no |  | The second argument. |
| `c` | `Object` | no |  | The third argument. |
| `d` | `Object` | no |  | The fourth argument. |

The method is also available as `SETTER()`.

Example: Call components inside the plugin
```html
<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>
```

### alias(name)

Type: `method`

Registers the plugin under another name in `PLUGINS`.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | The other name. |

Example: Use a short name
```js
plugin.alias('o');
console.log(PLUGINS.o === plugin);
// logs: true
```

### talker()

Type: `method`

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

The called plugin finds it in its `caller` property.

Example: Find out who called
```html
<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>
```

### on(name, callback)

Type: `method`

Registers a handler of a global event.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | The event name. |
| `callback` | `Function` | yes |  | The handler. |

Join several event names with `+`. See [Events](../events/).

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

### off(name, [callback])

Type: `method`

Removes a handler of a global event.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | The event name. |
| `callback` | `Function` | no |  | The handler to remove. |

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

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

Type: `method`

Emits a global event.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | The event name. |
| `a` | `Object` | no |  | The first argument. |
| `b` | `Object` | no |  | The second argument. |
| `c` | `Object` | no |  | The third argument. |
| `d` | `Object` | no |  | The fourth argument. |

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

Type: `method`

Sends a request.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `url` | `String` | yes |  | The method and the address. |
| `data` | `Object` | no |  | The data to send. |
| `callback` | `Function` | no |  | 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.

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

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

Type: `method`

Calls an operation of the API at the address in `DEF.api`.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | The name of the operation. |
| `data` | `Object` | no |  | The data to send. |
| `callback` | `Function` | no |  | A function `(response)`, or a path. |

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

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

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

Type: `method`

Calls an operation of the API at the address in `DEF.pipe`.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | The name of the operation. |
| `data` | `Object` | no |  | The data to send. |
| `callback` | `Function` | no |  | A function `(response)`, or a path. |

It works in the same way as `tapi()`.

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