---
title: Plugin
project: jComponent V19
version: 19.194
section: reference
language: en
license: MIT
url: https://totaljs.es/docs/jcomponent-v19/19-194/reference/plugin/
updated: 2026-11-18T13:58:29.465Z
---

# Plugin

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

A plugin instance is the object passed to the declaration of `PLUGIN()`. It holds the logic of a part of the page and works with the data under its own path. [Plugins](../../guides/plugins/) explains the concept, and [ui-plugin](../ui-plugin/) describes the element. In the examples, `plugin` is the instance of the `users` plugin.

## API

### exports.destroy = function()

Type: `delegate`

Runs when the plugin is removed.

Example: Releasing resources
```html
<ui-plugin path="users"><span></span></ui-plugin>

<script>
    PLUGIN('users', function(exports) {
        exports.destroy = () => console.log('destroyed');  // destroyed
    });
    setTimeout(() => PLUGINS.users.remove(), 500);
</script>
```

### plugin.name

Type: `property`

The path of the plugin.

Example: Reading name
```js
console.log(plugin.name);  // users
```

### plugin.id

Type: `property`

The identifier of the plugin.

It is the name with the `plug` prefix.

Example: Reading id
```js
console.log(plugin.id);  // plugusers
```

### plugin.element

Type: `property`

The element of the plugin as a jQuery object.

It is the element of the scope. A plugin without a scope uses `body`.

Example: Reading element
```js
console.log(plugin.element.prop('tagName'));  // UI-PLUGIN
```

### plugin.model

Type: `property`

Reads or replaces the whole model of the plugin.

Reading a missing model returns an empty object.

Example: Reading model
```js
plugin.model = { name: 'Peter' };
console.log(plugin.model.name);  // Peter
```

### plugin.data

Type: `property`

Reads or replaces the whole model of the plugin.

It is the same as `model`.

Example: Reading data
```js
plugin.data = { name: 'Anna' };
console.log(plugin.data.name);  // Anna
```

### plugin.form

Type: `property`

Returns the model and resets the state of its components.

Reading the property clears the touched and modified state of the components bound to the model.

Example: Reading form
```js
plugin.set('name', 'Peter');
console.log(plugin.form);  // {"name":"Peter"}
```

### plugin.modified

Type: `property`

Returns the fields changed by components.

Reading the property clears the touched and modified state of the components bound to the model.

Example: Reading modified
```js
FIND('field').modify('Peter');
console.log(plugin.modified);  // {"name":"Peter"}
```

### plugin.parent

Type: `property`

The plugin of the parent scope.

The value is `null` when there is no parent scope.

Example: Reading parent
```js
console.log(PLUGINS['users.detail'].parent.name);  // users
```

### plugin.caller

Type: `property`

The plugin that was active when this plugin was created.

The value is `null` when no plugin was active.

Example: Reading caller
```js
console.log(plugin.caller);  // null
```

### plugin.set([path], value, [type])

Type: `method`

Writes a value to a path of the plugin.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `path` | `String` | no |  | The relative path. |
| `value` | `Object` | yes |  | The value. |
| `type` | `Number` | no |  | The type of the change. A value of `10` or more is a delay in milliseconds. |

Returns: `Plugin` The plugin.

With one argument, the value replaces the whole model.

Example: Using set()
```js
plugin.set('name', 'Peter');
console.log(GET('users.name'));  // Peter
```

### plugin.get([path])

Type: `method`

Reads a path of the plugin.

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

Returns: `Object` The value.

Without an argument, the method returns the whole model.

Example: Using get()
```js
plugin.set('name', 'Peter');
console.log(plugin.get('name'));  // Peter
```

### plugin.nul(path, [type])

Type: `method`

Writes `null` to a path of the plugin.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `path` | `String` | yes |  | The relative path. |
| `type` | `Number` | no |  | The type of the change. A value of `10` or more is a delay in milliseconds. |

Returns: `Plugin` The plugin.

Example: Using nul()
```js
plugin.set('name', 'Peter');
plugin.nul('name');
console.log(plugin.get('name'));  // null
```

### plugin.push([path], value, [type])

Type: `method`

Adds an item to an array.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `path` | `String` | no |  | The relative path. |
| `value` | `Object` | yes |  | The item. |
| `type` | `Number` | no |  | The type of the change. A value of `10` or more is a delay in milliseconds. |

Returns: `Plugin` The plugin.

With one argument, the item is added to the model itself.

Example: Using push()
```js
plugin.push('items', 'A');
plugin.push('items', 'B');
console.log(plugin.get('items'));  // ["A","B"]
```

### plugin.extend([path], value, [type])

Type: `method`

Merges an object into a path.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `path` | `String` | no |  | The relative path. |
| `value` | `Object` | yes |  | The fields. |
| `type` | `Number` | no |  | The type of the change. A value of `10` or more is a delay in milliseconds. |

Returns: `Plugin` The plugin.

With one argument, the object is merged into the model itself.

Example: Using extend()
```js
plugin.extend('user', { name: 'Peter' });
plugin.extend('user', { age: 30 });
console.log(plugin.get('user'));  // {"name":"Peter","age":30}
```

### plugin.inc([path], value, [type])

Type: `method`

Increases a number.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `path` | `String` | no |  | The relative path. |
| `value` | `Number` | yes |  | The increment. |
| `type` | `Number` | no |  | The type of the change. A value of `10` or more is a delay in milliseconds. |

Returns: `Plugin` The plugin.

With one argument, the model itself is increased.

Example: Using inc()
```js
plugin.inc('count', 2);
plugin.inc('count', 3);
console.log(plugin.get('count'));  // 5
```

### plugin.toggle(path, [type])

Type: `method`

Inverts a boolean value.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `path` | `String` | yes |  | The relative path. |
| `type` | `Number` | no |  | The type of the change. A value of `10` or more is a delay in milliseconds. |

Returns: `Plugin` The plugin.

Example: Using toggle()
```js
plugin.toggle('open');
console.log(plugin.get('open'));  // true
```

### plugin.upd([path], [type])

Type: `method`

Notifies the listeners of a path.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `path` | `String` | no |  | The relative path. |
| `type` | `Number` | no |  | The type of the change. A value of `10` or more is a delay in milliseconds. |

Returns: `Plugin` The plugin.

Example: Using upd()
```js
plugin.watch('name', value => console.log(value));  // Peter
window.users.name = 'Peter';
plugin.upd('name');
```

### plugin.reset([path])

Type: `method`

Clears the state of components bound to a path.

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

Returns: `Plugin` The plugin.

The touched and modified state is cleared. The value does not change.

Example: Using reset()
```js
var com = FIND('field');
com.modify('Peter');
plugin.reset();
console.log(com.config.touched, GET('users.name'));  // false Peter
```

### plugin.watch(path, callback, [init])

Type: `method`

Watches a path of the plugin.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `path` | `String` | yes |  | The relative path. |
| `callback` | `Function` | yes |  | The function that receives the changes. |
| `init` | `Boolean` | no |  | `true` calls the callback immediately. |

Returns: `Plugin` The plugin.

The callback receives `value`, `path` and `type`. Several paths are separated by a comma or ` + `.

Example: Using watch()
```js
plugin.watch('name', (value, path) => console.log(value, path));  // Peter users.name
plugin.set('name', 'Peter');
```

### plugin.on(name, callback)

Type: `method`

Listens for a global event.

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

Returns: `Plugin` The plugin.

The scope of the plugin is active when the listener is registered.

Example: Using on()
```js
plugin.on('saved', value => console.log(value));  // 5
EMIT('saved', 5);
```

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

Type: `method`

Calls a method of the plugin.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | The name of the method. |

Returns: `Plugin` The plugin.

An unknown method is ignored. The method accepts up to five arguments.

Example: Using exec()
```js
plugin.hello = name => console.log('Hello ' + name);  // Hello Peter
plugin.exec('hello', 'Peter');
```

### plugin.makepath([path])

Type: `method`

Resolves a path relative to the plugin.

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

Returns: `String` The resolved path.

The forms are:

| Argument | Result for the `users` plugin |
| --- | --- |
| omitted | `users` |
| `name` or `?.name` | `users.name` |
| `*name` | `common.name` |
| `@flag` | `users @flag` |
| `^name` | `^users.name` |
| `+name` | `+users.name` |
| `\|name` | `users\|name` |
| `%name` | `%name` |
| `#name` | `#name` |

Example: Using makepath()
```js
console.log(plugin.makepath('name'));  // users.name
```

### plugin.format([text])

Type: `method`

Inserts the name of the plugin into a text.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `text` | `String` | no |  | The text. |

Returns: `String` The result.

The name replaces `{0}`. A text without a placeholder receives the name at the end.

Example: Using format()
```js
console.log(plugin.format('/api/{0}/'));  // /api/users/
```

### plugin.scope([path])

Type: `method`

Makes the plugin the active scope.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `path` | `String` | no |  | The scope to activate. |

Returns: `Plugin` The plugin.

Other functions then resolve `?` to this plugin. Another path can be passed, and `null` clears the active scope. The method is also available as `plugin()`.

Example: Using scope()
```js
plugin.scope();
console.log(M.scope());  // users
```

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

Type: `method`

Sends a request.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `url` | `String` | yes |  | The method and the URL. |
| `data` | `Object` | no |  | The data of the request. |
| `callback` | `Function|String` | no |  | A function or a relative path. |

Returns: `Plugin|Promise` The plugin, or a `Promise` without a callback.

The method calls `AJAX()`. A text callback is a path relative to the plugin, which receives the response. Without `data` and a callback, the method returns a `Promise`.

Example: Using ajax()
```js
plugin.ajax('GET /api/users/', 'response');
console.log(plugin.get('response.url'));  // GET /api/users/
```

### plugin.api(url, [data], [callback])

Type: `method`

Calls an API endpoint.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `url` | `String` | yes |  | The name of the endpoint. |
| `data` | `Object` | no |  | The data of the request. |
| `callback` | `Function|String` | no |  | A function or a relative path. |

Returns: `Plugin|Promise` The plugin, or a `Promise` without a callback.

The method calls `API()` and accepts the same callback forms as [`ajax()`](#ajax).

Example: Using api()
```js
plugin.api('users_read', function(response) {
    console.log(response);  // users_read
});
```

### plugin.dapi(name, [data], [callback])

Type: `method`

Calls a DAPI endpoint.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | The name of the endpoint. |
| `data` | `Object` | no |  | The data of the request. |
| `callback` | `Function|String` | no |  | A function or a relative path. |

Returns: `Plugin|Promise` The plugin, or a `Promise` without a callback.

The method calls `DAPI()` and accepts the same callback forms as [`ajax()`](#ajax).

Example: Using dapi()
```js
plugin.dapi('users', function(response) {
    console.log(response);  // users
});
```

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

Type: `method`

Calls a TAPI endpoint.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | The name of the endpoint. |
| `data` | `Object` | no |  | The data of the request. |
| `callback` | `Function|String` | no |  | A function or a relative path. |

Returns: `Plugin|Promise` The plugin, or a `Promise` without a callback.

The method calls `TAPI()` and accepts the same callback forms as [`ajax()`](#ajax).

Example: Using tapi()
```js
plugin.tapi('users', function(response) {
    console.log(response);  // users
});
```

### plugin.upload(url, data, [callback])

Type: `method`

Uploads data.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `url` | `String` | yes |  | The URL. |
| `data` | `FormData|Object` | yes |  | The data. |
| `callback` | `Function|String` | no |  | A function or a relative path. |

Returns: `Plugin|Promise` The plugin, or a `Promise` without a callback.

The method calls `UPLOAD()` and accepts the same callback forms as [`ajax()`](#ajax).

Example: Using upload()
```js
plugin.upload('/upload/', new FormData(), function(response) {
    console.log(response);  // /upload/
});
```

### plugin.remove()

Type: `method`

Removes the plugin.

Returns: `Boolean` Always `true`.

The `destroy` delegate runs, and the events and watchers registered during the declaration are removed. The element stays in the page.

Example: Using remove()
```js
console.log(plugin.remove(), PLUGINS.users === undefined);  // true true
```
