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

# Plugin

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

Un'istanza di plugin è il primo argomento della funzione passata a `PLUGIN()`, di solito chiamato `exports`. Viene creata per ogni elemento [ui-plugin](../ui-plugin/). I metodi lavorano con path relativi al plugin, come spiegato nella guida [Plugin](../../guides/plugins/). Gli esempi qui sotto usano un plugin con il path `orders`.

## API

### name

Type: `property`

Il path del plugin.

Returns: `String` Il path del plugin.

### path

Type: `property`

Il path del modello del plugin.

Returns: `Path` Il path del modello del plugin.

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

### config

Type: `property`

La configurazione del plugin.

Returns: `Object` La configurazione del plugin.

### element

Type: `property`

L'elemento del plugin come oggetto jQuery.

Returns: `jQuery` L'elemento del plugin come oggetto jQuery.

### dom

Type: `property`

L'elemento del plugin.

Returns: `HTMLElement` L'elemento del plugin.

### scope

Type: `property`

L'oggetto rispetto al quale vengono risolti i path, cioè `window`.

Returns: `Object` L'oggetto rispetto al quale vengono risolti i path, cioè `window`.

### cls

Type: `property`

Il valore dell'attributo `path` dell'elemento.

Returns: `String` Il valore dell'attributo `path` dell'elemento.

### ready

Type: `property`

È `true` quando il plugin è inizializzato.

Returns: `Boolean` È `true` quando il plugin è inizializzato.

### plugin

Type: `property`

Il plugin genitore di un plugin annidato con un `?` nel suo path, altrimenti `null`.

Returns: `Plugin` Il plugin genitore di un plugin annidato con un `?` nel suo path, altrimenti `null`.

### parent

Type: `property`

Lo stesso valore della proprietà `plugin`.

Returns: `Plugin` Lo stesso valore della proprietà `plugin`.

Example: Un plugin annidato
```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`

Il plugin che ha chiamato un metodo di questo plugin tramite `EXEC()`.

Returns: `Plugin` Il plugin che ha chiamato un metodo di questo plugin tramite `EXEC()`.

Vedi `talker()`.

### model

Type: `property`

Legge o sostituisce il modello del plugin.

Returns: `Object` Legge o sostituisce il modello del plugin.

L'assegnazione di un valore non notifica nulla di ciò che è associato al path. Per farlo, usa `set()` o `upd()`.

Example: Leggi il modello
```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`

Restituisce il modello e azzera lo stato dei suoi componenti.

Returns: `Object` Il modello del plugin.

Leggere la proprietà restituisce l'intero modello, come `get()`, e azzera lo stato toccato e modificato dei componenti nel plugin, come il flag `@reset`. Leggila dopo aver salvato un modulo.

Example: Salva un modulo
```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`

Lo stesso valore della proprietà `model`.

Returns: `Object` Lo stesso valore della proprietà `model`.

### form

Type: `property`

Restituisce il modello e azzera lo stato toccato e modificato dei componenti nel path del plugin.

Returns: `Object` Restituisce il modello e azzera lo stato toccato e modificato dei componenti nel path del plugin.

Example: Leggi un modulo e azzera il suo stato
```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`

Viene eseguito quando viene creato il plugin.

Example: Inizializza il 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`

Viene eseguito quando il plugin viene rimosso.

La pulizia periodica rimuove i plugin il cui elemento è uscito dalla pagina.

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

Legge un valore dal modello.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `path` | `String` | no |  | Un path relativo al plugin. |

Returns: `Object` Il valore.

Senza un path, il metodo restituisce l'intero modello. Un path con il flag `@reset` azzera anche lo stato dei componenti nel path.

Example: Leggi un valore
```js
plugin.set('user.name', 'Anna');
console.log(plugin.get('user.name'));
// logs: Anna
```

### set([path], value)

Type: `method`

Scrive un valore nel modello e notifica tutto ciò che è associato al path.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `path` | `String` | yes |  | Un path relativo al plugin. |
| `value` | `Object` | yes |  | Il valore da scrivere. |

Con un oggetto come unico argomento, il metodo sostituisce l'intero modello.

Example: Scrivi un valore
```js
plugin.set('status', 'open');
console.log(orders.status);
// logs: open
```

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

### push(path, value)

Type: `method`

Aggiunge un valore a un array nel modello.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `path` | `String` | yes |  | Un path relativo al plugin. |
| `value` | `Object` | yes |  | Il valore da scrivere. |

Ogni elemento di un valore di tipo array viene aggiunto separatamente.

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

### inc(path, [value])

Type: `method`

Incrementa un numero nel modello.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `path` | `String` | yes |  | Un path relativo al plugin. |
| `value` | `Number` | no | `1` | L'incremento. |

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

### toggle(path)

Type: `method`

Nega un valore nel modello.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `path` | `String` | yes |  | Un path relativo al plugin. |

Example: Inverti un valore
```js
plugin.toggle('open');
console.log(orders.open);
// logs: true
```

### nul(path)

Type: `method`

Scrive `null` nel modello.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `path` | `String` | yes |  | Un path relativo al plugin. |

Example: Svuota un valore
```js
plugin.set('user', 'Anna');
plugin.nul('user');
console.log(orders.user);
// logs: null
```

### upd(path)

Type: `method`

Notifica tutto ciò che è associato a un path, senza modificare il valore.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `path` | `String` | yes |  | Un path relativo al plugin. |

Il metodo è disponibile anche come `update()`.

Example: Annuncia una modifica diretta
```js
plugin.watch('count', value => console.log(value));
orders.count = 5;
plugin.upd('count');
// logs: 5
```

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

Type: `method`

Chiama una funzione a ogni modifica in un path del modello.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `path` | `String` | yes |  | Un path relativo al plugin. |
| `callback` | `Function` | yes |  | Una funzione `(value, flags, path)`. |
| `autoinit` | `Boolean` | no | `false` | Chiama subito la funzione con il valore corrente. |

Con una funzione come primo argomento, viene osservato l'intero modello. Unisci più path con `+`.

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

### unwatch(path, [callback])

Type: `method`

Rimuove gli osservatori registrati con `watch()`.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `path` | `String` | yes |  | Un path relativo al plugin. |
| `callback` | `Function` | no |  | L'osservatore da rimuovere. |

Senza `callback`, vengono rimossi tutti gli osservatori del path. Con una funzione come unico argomento, viene rimosso l'osservatore dell'intero modello. Unisci più path con `+`.

Example: Smetti di osservare un valore
```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`

Sostituisce il `?` in un path con il path del plugin.

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

Returns: `String` Il path risultante.

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

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

Type: `method`

Conserva un valore del modello nella cache tra una visita e l'altra.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `path` | `String` | yes |  | Un path relativo al plugin. |
| `expire` | `String` | yes |  | La durata del valore salvato, ad esempio `1 day`. |
| `def` | `Object|Function` | no |  | Il valore usato quando non c'è nulla di salvato. |

Come la funzione globale [CACHEPATH()](../globals/), con un path relativo al plugin.

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

### format(value)

Type: `method`

Inserisce il path del plugin in un testo.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `value` | `String` | yes |  | Il testo. |

Returns: `String` Il testo risultante.

Il testo `{0}` viene sostituito con il path. Senza `{0}`, il path viene aggiunto alla fine del testo.

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

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

Type: `method`

Chiama un metodo di questo plugin.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | Il nome del metodo. |
| `a` | `Object` | no |  | Il primo argomento. |
| `b` | `Object` | no |  | Il secondo argomento. |
| `c` | `Object` | no |  | Il terzo argomento. |
| `d` | `Object` | no |  | Il quarto argomento. |
| `e` | `Object` | no |  | Il quinto argomento. |

Il nome può contenere flag. Il metodo viene eseguito senza `this`, quindi fai riferimento al plugin con `exports`.

Example: Chiama un metodo
```js
plugin.exec('save', 1);
// logs: save 1
```

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

Type: `method`

Esegue un comando sui componenti all'interno dell'elemento del plugin.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | Il comando, eventualmente seguito da flag. |
| `a` | `Object` | no |  | Il primo argomento. |
| `b` | `Object` | no |  | Il secondo argomento. |
| `c` | `Object` | no |  | Il terzo argomento. |
| `d` | `Object` | no |  | Il quarto argomento. |

I componenti registrano i comandi con [command()](../component/). `CMD()` è lo stesso metodo. Per raggiungere i componenti dell'intera pagina, usa la funzione globale [CMD()](../globals/).

Example: Svuota gli elenchi di un 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`

Chiama un metodo dei componenti all'interno del plugin, nella forma `component/method`.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | La destinazione, ad esempio `grid/refresh`. |
| `a` | `Object` | no |  | Il primo argomento. |
| `b` | `Object` | no |  | Il secondo argomento. |
| `c` | `Object` | no |  | Il terzo argomento. |
| `d` | `Object` | no |  | Il quarto argomento. |

Il metodo è disponibile anche come `SETTER()`.

Example: Chiama i componenti all'interno del 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`

Registra il plugin con un altro nome in `PLUGINS`.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | L'altro nome. |

Example: Usa un nome breve
```js
plugin.alias('o');
console.log(PLUGINS.o === plugin);
// logs: true
```

### talker()

Type: `method`

Segna questo plugin come chiamante della prossima chiamata `EXEC()`.

Il plugin chiamato lo trova nella sua proprietà `caller`.

Example: Scopri chi ha chiamato
```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`

Registra un gestore di un evento globale.

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

Unisci più nomi di eventi con `+`. Vedi [Eventi](../events/).

Example: Gestisci un evento
```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`

Rimuove un gestore di un evento globale.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | Il nome dell'evento. |
| `callback` | `Function` | no |  | Il gestore da rimuovere. |

Senza un callback, vengono rimossi tutti i gestori dell'evento registrati su questo plugin.

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

Type: `method`

Emette un evento globale.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | Il nome dell'evento. |
| `a` | `Object` | no |  | Il primo argomento. |
| `b` | `Object` | no |  | Il secondo argomento. |
| `c` | `Object` | no |  | Il terzo argomento. |
| `d` | `Object` | no |  | Il quarto argomento. |

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

Type: `method`

Invia una richiesta.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `url` | `String` | yes |  | Il metodo e l'indirizzo. |
| `data` | `Object` | no |  | I dati da inviare. |
| `callback` | `Function` | no |  | Una funzione `(response)`, oppure un path. |

L'indirizzo ha la forma `METHOD /url`. Una stringa usata come callback è un path relativo al plugin che riceve la risposta.

Example: Memorizza la risposta nel modello
```js
plugin.ajax('GET /api/orders/', 'items');
console.log(orders.items.length);
// logs: 2
```

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

Type: `method`

Chiama un'operazione dell'API all'indirizzo in `DEF.api`.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | Il nome dell'operazione. |
| `data` | `Object` | no |  | I dati da inviare. |
| `callback` | `Function` | no |  | Una funzione `(response)`, oppure un path. |

Una stringa usata come callback è un path relativo al plugin che riceve la risposta.

Example: Chiama un'operazione dell'API
```js
DEF.api = '/api/';
plugin.tapi('orders_list', 'items');
// sends: POST /api/ {"schema":"orders_list"}
```

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

Type: `method`

Chiama un'operazione dell'API all'indirizzo in `DEF.pipe`.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | Il nome dell'operazione. |
| `data` | `Object` | no |  | I dati da inviare. |
| `callback` | `Function` | no |  | Una funzione `(response)`, oppure un path. |

Funziona allo stesso modo di `tapi()`.

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