Spanish Total.js
English
Documentation For AI agents
Contents

MAIN

The main object of the library, with its state and its low-level methods.

.md GitHub
On this page

MAIN is the main object of the library. M, Total and jComponent are other names for the same object. It holds the state of the library and low-level methods that work with any scope. For everyday code, use the global functions described with Paths.

API

class

MAIN.Binder

The class of data bindings.

See ui-bind.

property

MAIN.binders

The active data bindings.

Returns Array The active data bindings.

See ui-bind.

property

MAIN.caller

The plugin that was last called through EXEC().

Returns Plugin The plugin that was last called through EXEC().

Read the caller
<ui-plugin path="users"></ui-plugin>

<script>
PLUGIN('users', function(self) {
	self.refresh = NOOP;
});

ON('ready', function() {
	EXEC('users/refresh');
	console.log(MAIN.caller.name); // users
});
</script>
method

MAIN.cl(name, [callback])

The same function as CL().
ParameterTypeDescription
name String The names of the code lists.
callback? Function A function that runs when the lists are loaded.

See CL().

class

MAIN.Component

The class of components.
property

MAIN.components

The active component instances.

Returns Array The active component instances.

Count the components
<ui-component name="box"></ui-component>

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

ON('ready', function() {
	console.log(MAIN.components.length); // 1
});
</script>
property

MAIN.data

The storage of plugins with a generated path.

Returns Object The storage of plugins with a generated path.

property

MAIN.def

The same object as DEF.

Returns Object The same object as DEF.

See DEF.

Compare the objects
console.log(MAIN.def === DEF); // true
method

MAIN.emit(name, [a], [b], [c], [d])

The same function as EMIT().
ParameterTypeDescription
name String The name of the event.
a? Object The first argument.
b? Object The second argument.
c? Object The third argument.
d? Object The fourth argument.

See EMIT().

property

MAIN.events

The global event handlers, by event name.

Returns Object The global event handlers, by event name.

See Events.

method

MAIN.exec(name, [a], [b], [c], [d])

Runs the same logic as EXEC().
ParameterTypeDescription
name String The target of the call.
a? Object The first argument.
b? Object The second argument.
c? Object The third argument.
d? Object The fourth argument.

See EXEC().

method

MAIN.find(scope, path, callback)

Finds the components of a path in a scope.
ParameterTypeDescription
scope Object The scope, usually window.
path String The path, optionally with flags.
callback Function A function that receives the array of components.

Flags in the path narrow the result: @visible, @touched, @modified, @required, @invalid, @disabled and @enabled.

Find components
<ui-component name="field" path="form.name"></ui-component>

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

ON('ready', function() {
	MAIN.find(window, 'form', function(components) {
		console.log(components.length); // 1
	});
});
</script>
method

MAIN.flags(flags, [callback])

The same function as FLAGS().
ParameterTypeDescription
flags String The flags.
callback? Function A function that runs afterwards.

See FLAGS().

method

MAIN.free()

The same function as FREE().

See FREE().

method

MAIN.get(scope, path)

Reads a value from any object by a path.
ParameterTypeDescription
scope Object The object to read from.
path String The path inside the object.

Returns Object The value.

Read a nested value
var data = { user: { name: 'Peter' } };
console.log(MAIN.get(data, 'user.name')); // Peter
property

MAIN.is20

Identifies the major version of the library.

Returns Boolean Identifies the major version of the library.

The value is always true.

Read the value
console.log(MAIN.is20); // true
property

MAIN.loaded

Becomes true when the persistent cache is loaded.

Returns Boolean Becomes true when the persistent cache is loaded.

method

MAIN.newbinder(element, path, config, [callback])

Creates a data binding on an element.
ParameterTypeDescription
element jQuery The element.
path String The path.
config String The binding commands.
callback? Function A function that runs when the binding is created.
method

MAIN.newcomponent(element, name, path, config, [callback])

Creates a component on an element.
ParameterTypeDescription
element jQuery The element.
name String The name of the component.
path String The path.
config String The configuration.
callback? Function A function that runs when the component is created.
Create a component from code
<div id="target"></div>

<script>
COMPONENT('box', function(self) {
	self.make = function() {
		console.log(self.path.path); // form.name
	};
});

ON('ready', function() {
	MAIN.newcomponent($('#target'), 'box', 'form.name', '');
});
</script>
method

MAIN.newimporter(element, path, config)

Imports content into an element.
ParameterTypeDescription
element jQuery The element.
path String The address.
config String The configuration of the import.

It works like the ui-import element.

Import a part
<div id="target"></div>

<script>
ON('ready', function() {
	MAIN.newimporter($('#target'), '/parts/menu.html', '');

	setTimeout(function() {
		console.log($('#target').html()); // <b>Menu</b>
	}, 400);
});
</script>
method

MAIN.newplugin(element, path, config, [callback])

Creates a plugin on an element.
ParameterTypeDescription
element jQuery The element.
path String The name of the plugin.
config? String The configuration.
callback? Function A function that runs when the plugin is created.
Create a plugin from code
<div id="target"></div>

<script>
PLUGIN('users', function(self) {
	self.make = function() {
		console.log(self.name); // users
	};
});

ON('ready', function() {
	MAIN.newplugin($('#target'), 'users');
});
</script>
method

MAIN.notify(scope, path, [onlyflags])

Notifies the observers of a path in a scope.
ParameterTypeDescription
scope Object The scope, usually window.
path String The path, optionally with flags.
onlyflags? Boolean Limits the notification to the state of components. Default: false

With onlyflags, components only refresh their state, and watchers do not run.

Notify a path
WATCH('total', function(value) {
	console.log(value); // 5
});

window.total = 5;
MAIN.notify(window, 'total');
method

MAIN.off(name, [callback])

The same function as OFF().
ParameterTypeDescription
name String The name of the event.
callback? Function The handler to remove.

See OFF().

method

MAIN.on(name, callback, [init])

The same function as ON().
ParameterTypeDescription
name String The name of the event.
callback Function The handler.
init? Boolean Also calls the handler once. Default: false

See ON().

method

MAIN.parse(type, value, [format])

Converts a value to a type.
ParameterTypeDescription
type String The type.
value Object The value.
format? String The format of a date.

Returns Object The converted value.

The types are number, currency, float, boolean, bool, date and datetime. Numbers follow DEF.thousandsseparator and DEF.decimalseparator, and dates use DEF.dateformat when format is not set. A value that cannot be converted returns null. Other types return the value as it is.

Convert values
console.log(MAIN.parse('number', '1 200.5')); // 1200.5
console.log(MAIN.parse('boolean', 'yes')); // true
console.log(MAIN.parse('date', '2024-03-01') instanceof Date); // true
class

new MAIN.Path(path)

The class that parses paths.
ParameterTypeDescription
path String The path, optionally with flags.

See Path.

Parse a path
var path = new MAIN.Path('form.name @change');
console.log(path.path); // form.name
console.log(path.flags); // {"change":1}
class

MAIN.Plugin

The class of plugins.

See Plugin.

property

MAIN.plugins

The same object as PLUGINS.

Returns Object The same object as PLUGINS.

See PLUGINS.

method

MAIN.process(scope, value, target)

Delivers a value to a function or a path.
ParameterTypeDescription
scope Object The scope of a path.
value Object The value.
target Function|String A function or a path.

A function receives the value. A path is written and notified in the scope.

Deliver a value to a path
MAIN.process(window, 5, 'stats.total');
console.log(GET('stats.total')); // 5
property

MAIN.ready

Becomes true when the library has started.

Returns Boolean Becomes true when the library has started.

Check the state
ON('ready', function() {
	console.log(MAIN.ready); // true
});
method

MAIN.resize()

Measures the window again.

The method updates WW and WH. When the size has changed, it updates the device class on the body and emits the resize2 event.

property

MAIN.root

The window object, which is the scope of global paths.

Returns Object The window object, which is the scope of global paths.

Compare the objects
console.log(MAIN.root === window); // true
property

MAIN.scrollbars

The active custom scrollbars.

Returns Array The active custom scrollbars.

method

MAIN.seex(name, [a], [b], [c], [d])

Runs the same logic as SEEX().
ParameterTypeDescription
name String The target.
a? Object The first argument.
b? Object The second argument.
c? Object The third argument.
d? Object The fourth argument.

See SEEX().

method

MAIN.set(scope, path, value)

Writes a value to any object by a path.
ParameterTypeDescription
scope Object The object to write to.
path String The path inside the object.
value Object The value.

Missing objects along the path are created. Nothing is notified.

Write a nested value
var data = {};
MAIN.set(data, 'user.name', 'Peter');
console.log(data); // {"user":{"name":"Peter"}}
method

MAIN.setter(element, name, [a], [b], [c], [d])

Runs SETTER() for the components inside an element.
ParameterTypeDescription
element jQuery|Plugin The element or the plugin that limits the search.
name String The selector and the method.
a? Object The first argument.
b? Object The second argument.
c? Object The third argument.
d? Object The fourth argument.

Use null for all components. See SETTER().

Limit a call to an element
<div id="a"><ui-component name="box"></ui-component></div>
<div id="b"><ui-component name="box"></ui-component></div>

<script>
COMPONENT('box', function(self) {
	self.mark = function() {
		console.log(self.element.parent().attr('id')); // a
	};
});

ON('ready', function() {
	MAIN.setter($('#a'), 'box/mark');
});
</script>
property

MAIN.ua

The detected environment: os, browser and device.

Returns Object The detected environment: os, browser and device.

The device is desktop, tablet, mobile or tv. The value is available from the ready event onward.

Read the device
ON('ready', function() {
	console.log(Object.keys(MAIN.ua)); // ["os","browser","device"]
});
method

MAIN.unwatch(path, [callback])

The same function as UNWATCH().
ParameterTypeDescription
path String The path.
callback? Function The watcher to remove.
property

MAIN.version

The version of the library.

Returns Number The version of the library.

Read the version
console.log(MAIN.version); // 20.015
method

MAIN.watch(path, callback, [init])

The same function as WATCH().
ParameterTypeDescription
path String The path.
callback Function The watcher.
init? Boolean Also calls the watcher once. Default: false

See WATCH().

property

MAIN.watchers

The watchers registered with WATCH().

Returns Array The watchers registered with WATCH().

See WATCH().

Made in Europe