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

# Plugin

> Le proprietà, i delegate e i metodi di un'istanza di plugin.

Un'istanza di plugin è l'oggetto passato alla dichiarazione di `PLUGIN()`. Contiene la logica di una parte della pagina e lavora con i dati sotto il proprio path. [Plugin](../../guides/plugins/) spiega il concetto e [ui-plugin](../ui-plugin/) descrive l'elemento. Negli esempi, `plugin` è l'istanza del plugin `users`.

## API

### exports.destroy = function()

Type: `delegate`

Viene eseguito quando il plugin viene rimosso.

Example: Rilasciare le risorse
```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`

Il path del plugin.

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

### plugin.id

Type: `property`

L'identificatore del plugin.

È il nome con il prefisso `plug`.

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

### plugin.element

Type: `property`

L'elemento del plugin come oggetto jQuery.

È l'elemento dello scope. Un plugin senza scope usa `body`.

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

### plugin.model

Type: `property`

Legge o sostituisce l'intero modello del plugin.

La lettura di un modello mancante restituisce un oggetto vuoto.

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

### plugin.data

Type: `property`

Legge o sostituisce l'intero modello del plugin.

È uguale a `model`.

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

### plugin.form

Type: `property`

Restituisce il modello e reimposta lo stato dei suoi componenti.

La lettura della proprietà azzera lo stato di toccato e modificato dei componenti associati al modello.

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

### plugin.modified

Type: `property`

Restituisce i campi modificati dai componenti.

La lettura della proprietà azzera lo stato di toccato e modificato dei componenti associati al modello.

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

### plugin.parent

Type: `property`

Il plugin dello scope padre.

Il valore è `null` quando non esiste uno scope padre.

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

### plugin.caller

Type: `property`

Il plugin che era attivo quando è stato creato questo plugin.

Il valore è `null` quando nessun plugin era attivo.

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

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

Type: `method`

Scrive un valore in un path del plugin.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `path` | `String` | no |  | Il path relativo. |
| `value` | `Object` | yes |  | Il valore. |
| `type` | `Number` | no |  | Il tipo della modifica. Un valore pari o superiore a `10` è un ritardo in millisecondi. |

Returns: `Plugin` Il plugin.

Con un solo argomento, il valore sostituisce l'intero modello.

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

### plugin.get([path])

Type: `method`

Legge un path del plugin.

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

Returns: `Object` Il valore.

Senza argomenti, il metodo restituisce l'intero modello.

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

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

Type: `method`

Scrive `null` in un path del plugin.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `path` | `String` | yes |  | Il path relativo. |
| `type` | `Number` | no |  | Il tipo della modifica. Un valore pari o superiore a `10` è un ritardo in millisecondi. |

Returns: `Plugin` Il plugin.

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

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

Type: `method`

Aggiunge un elemento a un array.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `path` | `String` | no |  | Il path relativo. |
| `value` | `Object` | yes |  | L'elemento. |
| `type` | `Number` | no |  | Il tipo della modifica. Un valore pari o superiore a `10` è un ritardo in millisecondi. |

Returns: `Plugin` Il plugin.

Con un solo argomento, l'elemento viene aggiunto al modello stesso.

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

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

Type: `method`

Unisce un oggetto in un path.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `path` | `String` | no |  | Il path relativo. |
| `value` | `Object` | yes |  | I campi. |
| `type` | `Number` | no |  | Il tipo della modifica. Un valore pari o superiore a `10` è un ritardo in millisecondi. |

Returns: `Plugin` Il plugin.

Con un solo argomento, l'oggetto viene unito al modello stesso.

Example: Uso di 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`

Incrementa un numero.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `path` | `String` | no |  | Il path relativo. |
| `value` | `Number` | yes |  | L'incremento. |
| `type` | `Number` | no |  | Il tipo della modifica. Un valore pari o superiore a `10` è un ritardo in millisecondi. |

Returns: `Plugin` Il plugin.

Con un solo argomento, viene incrementato il modello stesso.

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

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

Type: `method`

Inverte un valore booleano.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `path` | `String` | yes |  | Il path relativo. |
| `type` | `Number` | no |  | Il tipo della modifica. Un valore pari o superiore a `10` è un ritardo in millisecondi. |

Returns: `Plugin` Il plugin.

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

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

Type: `method`

Notifica i listener di un path.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `path` | `String` | no |  | Il path relativo. |
| `type` | `Number` | no |  | Il tipo della modifica. Un valore pari o superiore a `10` è un ritardo in millisecondi. |

Returns: `Plugin` Il plugin.

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

### plugin.reset([path])

Type: `method`

Azzera lo stato dei componenti associati a un path.

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

Returns: `Plugin` Il plugin.

Lo stato di toccato e modificato viene azzerato. Il valore non cambia.

Example: Uso di 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`

Osserva un path del plugin.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `path` | `String` | yes |  | Il path relativo. |
| `callback` | `Function` | yes |  | La funzione che riceve le modifiche. |
| `init` | `Boolean` | no |  | `true` chiama subito il callback. |

Returns: `Plugin` Il plugin.

Il callback riceve `value`, `path` e `type`. Più path sono separati da una virgola o da ` + `.

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

### plugin.on(name, callback)

Type: `method`

Resta in ascolto di un evento globale.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | Il nome dell'evento. |
| `callback` | `Function` | yes |  | Il listener. |

Returns: `Plugin` Il plugin.

Lo scope del plugin è attivo quando il listener viene registrato.

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

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

Type: `method`

Chiama un metodo del plugin.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | Il nome del metodo. |

Returns: `Plugin` Il plugin.

Un metodo sconosciuto viene ignorato. Il metodo accetta fino a cinque argomenti.

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

### plugin.makepath([path])

Type: `method`

Risolve un path relativo al plugin.

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

Returns: `String` Il path risolto.

Le forme sono:

| Argomento | Risultato per il plugin `users` |
| --- | --- |
| omesso | `users` |
| `name` o `?.name` | `users.name` |
| `*name` | `common.name` |
| `@flag` | `users @flag` |
| `^name` | `^users.name` |
| `+name` | `+users.name` |
| `\|name` | `users\|name` |
| `%name` | `%name` |
| `#name` | `#name` |

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

### plugin.format([text])

Type: `method`

Inserisce il nome del plugin in un testo.

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

Returns: `String` Il risultato.

Il nome sostituisce `{0}`. Un testo senza segnaposto riceve il nome alla fine.

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

### plugin.scope([path])

Type: `method`

Rende il plugin lo scope attivo.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `path` | `String` | no |  | Lo scope da attivare. |

Returns: `Plugin` Il plugin.

Le altre funzioni risolvono allora `?` verso questo plugin. Puoi passare un altro path, e `null` azzera lo scope attivo. Il metodo è disponibile anche come `plugin()`.

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

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

Type: `method`

Invia una richiesta.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `url` | `String` | yes |  | Il metodo e l'URL. |
| `data` | `Object` | no |  | I dati della richiesta. |
| `callback` | `Function|String` | no |  | Una funzione o un path relativo. |

Returns: `Plugin|Promise` Il plugin, oppure una `Promise` senza callback.

Il metodo chiama `AJAX()`. Un callback testuale è un path relativo al plugin, che riceve la risposta. Senza `data` e senza callback, il metodo restituisce una `Promise`.

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

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

Type: `method`

Chiama un endpoint API.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `url` | `String` | yes |  | Il nome dell'endpoint. |
| `data` | `Object` | no |  | I dati della richiesta. |
| `callback` | `Function|String` | no |  | Una funzione o un path relativo. |

Returns: `Plugin|Promise` Il plugin, oppure una `Promise` senza callback.

Il metodo chiama `API()` e accetta le stesse forme di callback di [`ajax()`](#ajax).

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

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

Type: `method`

Chiama un endpoint DAPI.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | Il nome dell'endpoint. |
| `data` | `Object` | no |  | I dati della richiesta. |
| `callback` | `Function|String` | no |  | Una funzione o un path relativo. |

Returns: `Plugin|Promise` Il plugin, oppure una `Promise` senza callback.

Il metodo chiama `DAPI()` e accetta le stesse forme di callback di [`ajax()`](#ajax).

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

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

Type: `method`

Chiama un endpoint TAPI.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | Il nome dell'endpoint. |
| `data` | `Object` | no |  | I dati della richiesta. |
| `callback` | `Function|String` | no |  | Una funzione o un path relativo. |

Returns: `Plugin|Promise` Il plugin, oppure una `Promise` senza callback.

Il metodo chiama `TAPI()` e accetta le stesse forme di callback di [`ajax()`](#ajax).

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

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

Type: `method`

Carica dati.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `url` | `String` | yes |  | L'URL. |
| `data` | `FormData|Object` | yes |  | I dati. |
| `callback` | `Function|String` | no |  | Una funzione o un path relativo. |

Returns: `Plugin|Promise` Il plugin, oppure una `Promise` senza callback.

Il metodo chiama `UPLOAD()` e accetta le stesse forme di callback di [`ajax()`](#ajax).

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

### plugin.remove()

Type: `method`

Rimuove il plugin.

Returns: `Boolean` Sempre `true`.

Viene eseguito il delegate `destroy` e vengono rimossi gli eventi e gli osservatori registrati durante la dichiarazione. L'elemento resta nella pagina.

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