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

# Eventi

> Gli eventi emessi dalla libreria.

La libreria emette questi eventi. Puoi catturarli con [ON()](../globals/) ed emettere i tuoi con [EMIT()](../globals/). Anche i [componenti](../component/) e i [plugin](../plugin/) possono catturare gli stessi eventi con il proprio metodo `on()`.

## API

### ON('@flag name', function(path) {})

Type: `event`

Viene eseguito quando un flag è usato in un path, in una chiamata o in `FLAGS()`.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `path` | `String` | yes |  | Il path che contiene il flag. È vuoto quando non ce n'è nessuno. |

Sostituisci `name` con il nome del flag. Vedi [Flag e modificatori di path](../../guides/flags/).

Example: Mostra un indicatore di caricamento
```js
ON('@flag loading', function() {
	console.log('loading'); // loading
});

SET('form.name @loading', 'Peter');
```

### ON('bind', function(binder) {})

Type: `event`

Viene eseguito quando viene creata un'associazione dei dati.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `binder` | `Object` | yes |  | L'associazione. |

Vedi [ui-bind](../ui-bind/).

Example: Conta le associazioni
```html
<ui-bind path="form.name" config="text:value"><span></span></ui-bind>

<script>
ON('bind', function() {
	console.log(MAIN.binders.length); // 1
});
</script>
```

### ON('component', function(component) {})

Type: `event`

Viene eseguito quando viene creato un componente.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `component` | `Component` | yes |  | L'istanza. |

Vedi [Componente](../component/).

Example: Registra nel log i componenti creati
```html
<ui-component name="box"></ui-component>

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

ON('component', function(component) {
	console.log(component.name); // box
});
</script>
```

### ON('env', function(name, value) {})

Type: `event`

Viene eseguito quando `ENV()` scrive una variabile.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | Il nome della variabile. |
| `value` | `Object` | yes |  | Il nuovo valore. |

Un gestore che scrive una variabile vede il nuovo valore nei propri argomenti.

Example: Osserva una variabile
```js
ON('env', function(name, value) {
	console.log(name, value); // title Demo
});

ENV('title', 'Demo');
```

### ON('ERROR', function(response) {})

Type: `event`

Viene eseguito quando una risposta contiene un errore.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `response` | `Object|String` | yes |  | La risposta o lo stato. |

L'evento viene emesso da [ERROR()](../globals/) e da [AJAX()](../globals/) con la parola chiave `ERROR`. Per una richiesta fallita senza corpo, l'argomento è lo stato HTTP sotto forma di stringa.

Example: Gestisci gli errori in un unico punto
```js
ON('ERROR', function(response) {
	console.log(response[0].error); // Invalid name
});

ERROR([{ error: 'Invalid name' }]);
```

Example: Una richiesta fallita
```js
ON('ERROR', function(status) {
	console.log(status); // 500
});

AJAX('GET /api/fail/ ERROR', NOOP);
```

### ON('import', function(url, target) {})

Type: `event`

Viene eseguito quando `IMPORT()` ha caricato i contenuti.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `url` | `String` | yes |  | L'indirizzo. |
| `target` | `jQuery|String` | yes |  | L'elemento script o link, oppure la destinazione dei contenuti HTML. |

Example: Osserva le importazioni
```html
<div id="menu"></div>

<script>
ON('import', function(url, target) {
	console.log(url, target); // /parts/menu.html #menu
});

IMPORT('/parts/menu.html', '#menu');
</script>
```

### ON('init', function() {})

Type: `event`

Viene eseguito per primo durante l'avvio della libreria.

La cache persistente non è ancora caricata. Un gestore registrato dopo l'avvio viene eseguito immediatamente.

Example: Prepara l'app
```js
ON('init', function() {
	console.log(MAIN.ready); // false
});
```

### ON('knockknock', function(counter) {})

Type: `event`

Viene eseguito ogni minuto.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `counter` | `Number` | yes |  | Il numero di minuti dall'apertura della pagina. |

Viene eseguito subito dopo l'evento `service`.

Example: Esegui un'attività periodica
```js
ON('knockknock', function(counter) {
	console.log(counter); // 1
});
```

### ON('plugin', function(plugin) {})

Type: `event`

Viene eseguito quando viene creato un plugin.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `plugin` | `Plugin` | yes |  | L'istanza. |

Vedi [Plugin](../plugin/).

Example: Registra nel log i plugin creati
```html
<ui-plugin path="users"></ui-plugin>

<script>
PLUGIN('users', function(self) {});

ON('plugin', function(plugin) {
	console.log(plugin.name); // users
});
</script>
```

### ON('ready', function() {})

Type: `event`

Viene eseguito quando la libreria e i componenti della pagina sono pronti.

Un gestore registrato dopo quel momento viene eseguito immediatamente.

Example: Avvia l'app
```html
<ui-component name="box"></ui-component>

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

ON('ready', function() {
	console.log(FIND('box').name); // box
});
</script>
```

### ON('request', function(options) {})

Type: `event`

Viene eseguito per ogni chiamata `AJAX()`, prima dell'invio della richiesta.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `options` | `Object` | yes |  | La richiesta: `id`, `method`, `url`, `headers`, `data`, `scope`, `cancel`, `respond()` e, una volta inviata la richiesta, `xhr`. |

Il gestore può modificare `options.url`, `options.method`, `options.headers` e `options.data`. Imposta `options.cancel = true` per bloccare la richiesta. In questo caso, il callback non viene eseguito. Chiama `options.respond(value)` per rispondere alla chiamata senza inviare una richiesta: il callback riceve `value`. Una volta inviata la richiesta, `options.xhr` contiene il suo `XMLHttpRequest`.

Example: Aggiungi un token a ogni richiesta
```js
ON('request', function(options) {
	options.headers['x-token'] = '123';
	console.log(options.method, options.url); // GET /api/users/
});

AJAX('GET /api/users/', NOOP);
```

Example: Rispondi senza una richiesta
```js
ON('request', function(options) {
	if (options.url === '/api/config/')
		options.respond({ theme: 'dark' });
});

AJAX('GET /api/config/', function(response) {
	console.log(response.theme); // dark
});
```

### ON('resize2', function() {})

Type: `event`

Viene eseguito quando le dimensioni della finestra sono cambiate.

I valori di [WW](../globals/) e [WH](../globals/) sono già aggiornati.

Example: Reagisci alle nuove dimensioni
```js
ON('resize2', function() {
	console.log(typeof(WW)); // number
});
```

### ON('response', function(options) {})

Type: `event`

Viene eseguito per ogni risposta di `AJAX()`, prima del callback.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `options` | `Object` | yes |  | La richiesta insieme a `status`, `text`, `response`, `iserror`, `duration` e `headers`. |

Il gestore può modificare `options.response`. Imposta `options.cancel = true` per saltare il callback.

Example: Registra nel log le risposte
```js
ON('response', function(options) {
	console.log(options.status, options.iserror); // 200 false
});

AJAX('GET /api/users/', NOOP);
```

### ON('scroll', function(area) {})

Type: `event`

Viene eseguito quando inizia lo scorrimento in una barra di scorrimento personalizzata.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `area` | `jQuery` | yes |  | L'elemento che scorre. |

Consulta [SCROLLBAR()](../globals/).

Example: Rileva lo scorrimento
```html
<div id="panel" style="width:200px;height:100px"><div style="height:600px">Content</div></div>

<script>
ON('scroll', function(area) {
	console.log(area instanceof jQuery); // true
});

var scrollbar = SCROLLBAR($('#panel'));
scrollbar.area.css('overflow', 'auto');
scrollbar.scrollTop(20);
</script>
```

### ON('scrollidle', function(area) {})

Type: `event`

Viene eseguito quando lo scorrimento in una barra di scorrimento personalizzata si è fermato.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `area` | `jQuery` | yes |  | L'elemento che scorre. |

L'evento arriva 700 millisecondi dopo l'ultimo movimento.

Example: Rileva la fine dello scorrimento
```html
<div id="panel" style="width:200px;height:100px"><div style="height:600px">Content</div></div>

<script>
ON('scrollidle', function(area) {
	console.log(area.scrollTop()); // 20
});

var scrollbar = SCROLLBAR($('#panel'));
scrollbar.area.css('overflow', 'auto');
scrollbar.scrollTop(20);
</script>
```

### ON('service', function(counter) {})

Type: `event`

Viene eseguito ogni minuto.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `counter` | `Number` | yes |  | Il numero di minuti dall'apertura della pagina. |

La libreria aggiorna prima [NOW](../globals/), rimuove gli elementi scaduti della cache e svuota [TEMP](../globals/).

Example: Esegui un'attività periodica
```js
ON('service', function(counter) {
	console.log(counter); // 1
});
```

### ON('visible', function(visible) {})

Type: `event`

Viene eseguito quando la pagina passa in background o ne ritorna.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `visible` | `Boolean` | yes |  | `true` quando la pagina è visibile. |

Example: Sospendi il lavoro in background
```js
ON('visible', function(visible) {
	console.log(visible); // true
});
```
