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

# Eventos

> Los eventos que emite la biblioteca.

La biblioteca emite estos eventos. Captúralos con [ON()](../globals/) y emite los tuyos con [EMIT()](../globals/). Los [componentes](../component/) y los [plugins](../plugin/) pueden capturar los mismos eventos con su propio método `on()`.

## API

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

Type: `event`

Se ejecuta cuando se usa un flag en un path, en una llamada o en `FLAGS()`.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `path` | `String` | yes |  | El path que lleva el flag. Está vacío cuando no hay ninguno. |

Sustituye `name` por el nombre del flag. Consulta [Flags y modificadores de path](../../guides/flags/).

Example: Mostrar un indicador de carga
```js
ON('@flag loading', function() {
	console.log('loading'); // loading
});

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

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

Type: `event`

Se ejecuta cuando se crea un enlace de datos.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `binder` | `Object` | yes |  | El enlace. |

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

Example: Contar los enlaces
```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`

Se ejecuta cuando se crea un componente.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `component` | `Component` | yes |  | La instancia. |

Consulta [Component](../component/).

Example: Registrar los componentes creados
```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`

Se ejecuta cuando `ENV()` escribe una variable.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | El nombre de la variable. |
| `value` | `Object` | yes |  | El nuevo valor. |

Un manejador que escribe una variable ve el nuevo valor en sus argumentos.

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

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

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

Type: `event`

Se ejecuta cuando una respuesta contiene un error.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `response` | `Object|String` | yes |  | La respuesta o el estado. |

El evento lo emiten [ERROR()](../globals/) y [AJAX()](../globals/) con la palabra clave `ERROR`. En una petición fallida sin cuerpo, el argumento es el estado HTTP como cadena.

Example: Gestionar los errores en un solo lugar
```js
ON('ERROR', function(response) {
	console.log(response[0].error); // Invalid name
});

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

Example: Una petición fallida
```js
ON('ERROR', function(status) {
	console.log(status); // 500
});

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

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

Type: `event`

Se ejecuta cuando `IMPORT()` ha cargado el contenido.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `url` | `String` | yes |  | La dirección. |
| `target` | `jQuery|String` | yes |  | El elemento script o link, o el destino del contenido HTML. |

Example: Observar las importaciones
```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`

Se ejecuta primero mientras arranca la biblioteca.

La caché persistente todavía no está cargada. Un manejador registrado después del arranque se ejecuta de inmediato.

Example: Preparar la aplicación
```js
ON('init', function() {
	console.log(MAIN.ready); // false
});
```

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

Type: `event`

Se ejecuta cada minuto.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `counter` | `Number` | yes |  | El número de minutos desde el inicio de la página. |

Se ejecuta justo después del evento `service`.

Example: Ejecutar una tarea periódica
```js
ON('knockknock', function(counter) {
	console.log(counter); // 1
});
```

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

Type: `event`

Se ejecuta cuando se crea un plugin.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `plugin` | `Plugin` | yes |  | La instancia. |

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

Example: Registrar los plugins creados
```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`

Se ejecuta cuando la biblioteca y los componentes de la página están listos.

Un manejador registrado después de ese momento se ejecuta de inmediato.

Example: Iniciar la aplicación
```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`

Se ejecuta en cada llamada a `AJAX()`, antes de enviar la petición.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `options` | `Object` | yes |  | La petición: `id`, `method`, `url`, `headers`, `data`, `scope`, `cancel`, `respond()` y, una vez enviada la petición, `xhr`. |

El manejador puede cambiar `options.url`, `options.method`, `options.headers` y `options.data`. Establece `options.cancel = true` para detener la petición. En ese caso, el callback no se ejecuta. Llama a `options.respond(value)` para responder a la llamada sin enviar ninguna petición: el callback recibe `value`. Una vez enviada la petición, `options.xhr` contiene su `XMLHttpRequest`.

Example: Añadir un token a cada petición
```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: Responder sin una petición
```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`

Se ejecuta cuando ha cambiado el tamaño de la ventana.

Los valores de [WW](../globals/) y [WH](../globals/) ya están actualizados.

Example: Reaccionar a un nuevo tamaño
```js
ON('resize2', function() {
	console.log(typeof(WW)); // number
});
```

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

Type: `event`

Se ejecuta en cada respuesta de `AJAX()`, antes del callback.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `options` | `Object` | yes |  | La petición junto con `status`, `text`, `response`, `iserror`, `duration` y `headers`. |

El manejador puede cambiar `options.response`. Establece `options.cancel = true` para omitir el callback.

Example: Registrar las respuestas
```js
ON('response', function(options) {
	console.log(options.status, options.iserror); // 200 false
});

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

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

Type: `event`

Se ejecuta cuando empieza el desplazamiento en una barra de desplazamiento personalizada.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `area` | `jQuery` | yes |  | El elemento que se desplaza. |

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

Example: Detectar el desplazamiento
```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`

Se ejecuta cuando se ha detenido el desplazamiento en una barra de desplazamiento personalizada.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `area` | `jQuery` | yes |  | El elemento que se desplaza. |

El evento llega 700 milisegundos después del último movimiento.

Example: Detectar el final del desplazamiento
```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`

Se ejecuta cada minuto.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `counter` | `Number` | yes |  | El número de minutos desde el inicio de la página. |

La biblioteca actualiza primero [NOW](../globals/), elimina los elementos caducados de la caché y vacía [TEMP](../globals/).

Example: Ejecutar una tarea periódica
```js
ON('service', function(counter) {
	console.log(counter); // 1
});
```

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

Type: `event`

Se ejecuta cuando la página pasa a segundo plano o vuelve de él.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `visible` | `Boolean` | yes |  | `true` cuando la página está visible. |

Example: Pausar el trabajo en segundo plano
```js
ON('visible', function(visible) {
	console.log(visible); // true
});
```
