Events
The events emitted by the library.
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().
ON('@flag name', function(path) {})| Parameter | Type | Description |
|---|---|---|
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.
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.
ON('bind', function(binder) {})| Parameter | Type | Description |
|---|---|---|
binder |
Object |
The binding. |
See ui-bind.
<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.
ON('component', function(component) {})| Parameter | Type | Description |
|---|---|---|
component |
Component |
The instance. |
See Component.
<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.
ON('env', function(name, value) {})| Parameter | Type | Description |
|---|---|---|
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.
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.
ON('ERROR', function(response) {})| Parameter | Type | Description |
|---|---|---|
response |
Object|String |
The response or the status. |
ON('ERROR', function(response) {
console.log(response[0].error); // Invalid name
});
ERROR([{ error: 'Invalid name' }]);
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.
ON('import', function(url, target) {})| Parameter | Type | Description |
|---|---|---|
url |
String |
The address. |
target |
jQuery|String |
The script or link element, or the target of HTML content. |
<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.
ON('init', function() {})The persistent cache is not loaded yet. A handler registered after the start runs immediately.
ON('init', function() {
console.log(MAIN.ready); // false
});
event
ON('knockknock', function(counter) {})
Runs every minute.
ON('knockknock', function(counter) {})| Parameter | Type | Description |
|---|---|---|
counter |
Number |
The number of minutes from the start of the page. |
It runs right after the service event.
ON('knockknock', function(counter) {
console.log(counter); // 1
});
event
ON('plugin', function(plugin) {})
Runs when a plugin is created.
ON('plugin', function(plugin) {})| Parameter | Type | Description |
|---|---|---|
plugin |
Plugin |
The instance. |
See Plugin.
<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.
ON('ready', function() {})A handler registered after that moment runs immediately.
<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.
ON('request', function(options) {})| Parameter | Type | Description |
|---|---|---|
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.
ON('request', function(options) {
options.headers['x-token'] = '123';
console.log(options.method, options.url); // GET /api/users/
});
AJAX('GET /api/users/', NOOP);
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.
ON('resize2', function() {})
event
ON('response', function(options) {})
Runs for every AJAX() response, ahead of the callback.
ON('response', function(options) {})| Parameter | Type | Description |
|---|---|---|
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.
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.
ON('scroll', function(area) {})| Parameter | Type | Description |
|---|---|---|
area |
jQuery |
The scrolling element. |
See SCROLLBAR().
<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.
ON('scrollidle', function(area) {})| Parameter | Type | Description |
|---|---|---|
area |
jQuery |
The scrolling element. |
The event comes 700 milliseconds after the last movement.
<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.
ON('service', function(counter) {})
event
ON('visible', function(visible) {})
Runs when the page moves to the background or returns from it.
ON('visible', function(visible) {})| Parameter | Type | Description |
|---|---|---|
visible |
Boolean |
true when the page is visible. |
ON('visible', function(visible) {
console.log(visible); // true
});