Spanish Total.js
English
Documentation For AI agents
Contents

Events

The events emitted by the library.

.md GitHub
On this page

The library emits these events. Capture them with ON() and emit your own with EMIT(). Components and plugins can capture the same events with their own on() method.

API

event

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

Runs when a flag is used in a path, a call or FLAGS().
ParameterTypeDescription
path String The path that carries the flag. It is empty when there is none.

Replace name with the name of the flag. See Flags and path modifiers.

Show a loading indicator
ON('@flag loading', function() {
	console.log('loading'); // loading
});

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

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

Runs when a data binding is created.
ParameterTypeDescription
binder Object The binding.

See ui-bind.

Count the bindings
<ui-bind path="form.name" config="text:value"><span></span></ui-bind>

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

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

Runs when a component is created.
ParameterTypeDescription
component Component The instance.
Log the created components
<ui-component name="box"></ui-component>

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

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

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

Runs when ENV() writes a variable.
ParameterTypeDescription
name String The name of the variable.
value Object The new value.

A handler that writes one variable sees the new value in its arguments.

Observe a variable
ON('env', function(name, value) {
	console.log(name, value); // title Demo
});

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

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

Runs when a response holds an error.
ParameterTypeDescription
response Object|String The response or the status.

The event is emitted by ERROR() and by AJAX() with the ERROR keyword. For a failed request without a body, the argument is the HTTP status as a string.

Handle errors in one place
ON('ERROR', function(response) {
	console.log(response[0].error); // Invalid name
});

ERROR([{ error: 'Invalid name' }]);
A failed request
ON('ERROR', function(status) {
	console.log(status); // 500
});

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

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

Runs when IMPORT() has loaded content.
ParameterTypeDescription
url String The address.
target jQuery|String The script or link element, or the target of HTML content.
Observe imports
<div id="menu"></div>

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

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

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

Runs first while the library starts.

The persistent cache is not loaded yet. A handler registered after the start runs immediately.

Prepare the app
ON('init', function() {
	console.log(MAIN.ready); // false
});
event

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

Runs every minute.
ParameterTypeDescription
counter Number The number of minutes from the start of the page.

It runs right after the service event.

Run a periodic task
ON('knockknock', function(counter) {
	console.log(counter); // 1
});
event

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

Runs when a plugin is created.
ParameterTypeDescription
plugin Plugin The instance.

See Plugin.

Log the created plugins
<ui-plugin path="users"></ui-plugin>

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

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

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

Runs when the library and the components on the page are ready.

A handler registered after that moment runs immediately.

Start the app
<ui-component name="box"></ui-component>

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

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

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

Runs for every AJAX() call, ahead of sending the request.
ParameterTypeDescription
options Object The request: id, method, url, headers, data, scope, cancel, respond() and, once the request is sent, xhr.

The handler can change options.url, options.method, options.headers and options.data. Set options.cancel = true to stop the request. In that case, the callback does not run. Call options.respond(value) to answer the call without sending a request: the callback receives value. Once the request is sent, options.xhr holds its XMLHttpRequest.

Add a token to each request
ON('request', function(options) {
	options.headers['x-token'] = '123';
	console.log(options.method, options.url); // GET /api/users/
});

AJAX('GET /api/users/', NOOP);
Answer without a request
ON('request', function(options) {
	if (options.url === '/api/config/')
		options.respond({ theme: 'dark' });
});

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

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

Runs when the size of the window has changed.

The values of WW and WH are already updated.

React to a new size
ON('resize2', function() {
	console.log(typeof(WW)); // number
});
event

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

Runs for every AJAX() response, ahead of the callback.
ParameterTypeDescription
options Object The request together with status, text, response, iserror, duration and headers.

The handler can change options.response. Set options.cancel = true to skip the callback.

Log the responses
ON('response', function(options) {
	console.log(options.status, options.iserror); // 200 false
});

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

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

Runs when scrolling starts in a custom scrollbar.
ParameterTypeDescription
area jQuery The scrolling element.
Detect scrolling
<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>
event

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

Runs when scrolling in a custom scrollbar has stopped.
ParameterTypeDescription
area jQuery The scrolling element.

The event comes 700 milliseconds after the last movement.

Detect the end of scrolling
<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>
event

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

Runs every minute.
ParameterTypeDescription
counter Number The number of minutes from the start of the page.

The library refreshes NOW, removes expired cache items and clears TEMP first.

Run a periodic task
ON('service', function(counter) {
	console.log(counter); // 1
});
event

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

Runs when the page moves to the background or returns from it.
ParameterTypeDescription
visible Boolean true when the page is visible.
Pause work in the background
ON('visible', function(visible) {
	console.log(visible); // true
});
Made in Europe