Spanish Total.js
English
Documentation For AI agents
Contents

Plugin

The properties, delegates and methods of a plugin instance.

.md GitHub
On this page

A plugin instance is the object passed to the declaration of PLUGIN(). It holds the logic of a part of the page and works with the data under its own path. Plugins explains the concept, and ui-plugin describes the element. In the examples, plugin is the instance of the users plugin.

API

delegate

exports.destroy = function()

Runs when the plugin is removed.
Releasing resources
<ui-plugin path="users"><span></span></ui-plugin>

<script>
    PLUGIN('users', function(exports) {
        exports.destroy = () => console.log('destroyed');  // destroyed
    });
    setTimeout(() => PLUGINS.users.remove(), 500);
</script>
property

plugin.name

The path of the plugin.
Reading name
console.log(plugin.name);  // users
property

plugin.id

The identifier of the plugin.

It is the name with the plug prefix.

Reading id
console.log(plugin.id);  // plugusers
property

plugin.element

The element of the plugin as a jQuery object.

It is the element of the scope. A plugin without a scope uses body.

Reading element
console.log(plugin.element.prop('tagName'));  // UI-PLUGIN
property

plugin.model

Reads or replaces the whole model of the plugin.

Reading a missing model returns an empty object.

Reading model
plugin.model = { name: 'Peter' };
console.log(plugin.model.name);  // Peter
property

plugin.data

Reads or replaces the whole model of the plugin.

It is the same as model.

Reading data
plugin.data = { name: 'Anna' };
console.log(plugin.data.name);  // Anna
property

plugin.form

Returns the model and resets the state of its components.

Reading the property clears the touched and modified state of the components bound to the model.

Reading form
plugin.set('name', 'Peter');
console.log(plugin.form);  // {"name":"Peter"}
property

plugin.modified

Returns the fields changed by components.

Reading the property clears the touched and modified state of the components bound to the model.

Reading modified
FIND('field').modify('Peter');
console.log(plugin.modified);  // {"name":"Peter"}
property

plugin.parent

The plugin of the parent scope.

The value is null when there is no parent scope.

Reading parent
console.log(PLUGINS['users.detail'].parent.name);  // users
property

plugin.caller

The plugin that was active when this plugin was created.

The value is null when no plugin was active.

Reading caller
console.log(plugin.caller);  // null
method

plugin.set([path], value, [type])

Writes a value to a path of the plugin.
ParameterTypeDescription
path? String The relative path.
value Object The value.
type? Number The type of the change. A value of 10 or more is a delay in milliseconds.

Returns Plugin The plugin.

With one argument, the value replaces the whole model.

Using set()
plugin.set('name', 'Peter');
console.log(GET('users.name'));  // Peter
method

plugin.get([path])

Reads a path of the plugin.
ParameterTypeDescription
path? String The relative path.

Returns Object The value.

Without an argument, the method returns the whole model.

Using get()
plugin.set('name', 'Peter');
console.log(plugin.get('name'));  // Peter
method

plugin.nul(path, [type])

Writes null to a path of the plugin.
ParameterTypeDescription
path String The relative path.
type? Number The type of the change. A value of 10 or more is a delay in milliseconds.

Returns Plugin The plugin.

Using nul()
plugin.set('name', 'Peter');
plugin.nul('name');
console.log(plugin.get('name'));  // null
method

plugin.push([path], value, [type])

Adds an item to an array.
ParameterTypeDescription
path? String The relative path.
value Object The item.
type? Number The type of the change. A value of 10 or more is a delay in milliseconds.

Returns Plugin The plugin.

With one argument, the item is added to the model itself.

Using push()
plugin.push('items', 'A');
plugin.push('items', 'B');
console.log(plugin.get('items'));  // ["A","B"]
method

plugin.extend([path], value, [type])

Merges an object into a path.
ParameterTypeDescription
path? String The relative path.
value Object The fields.
type? Number The type of the change. A value of 10 or more is a delay in milliseconds.

Returns Plugin The plugin.

With one argument, the object is merged into the model itself.

Using extend()
plugin.extend('user', { name: 'Peter' });
plugin.extend('user', { age: 30 });
console.log(plugin.get('user'));  // {"name":"Peter","age":30}
method

plugin.inc([path], value, [type])

Increases a number.
ParameterTypeDescription
path? String The relative path.
value Number The increment.
type? Number The type of the change. A value of 10 or more is a delay in milliseconds.

Returns Plugin The plugin.

With one argument, the model itself is increased.

Using inc()
plugin.inc('count', 2);
plugin.inc('count', 3);
console.log(plugin.get('count'));  // 5
method

plugin.toggle(path, [type])

Inverts a boolean value.
ParameterTypeDescription
path String The relative path.
type? Number The type of the change. A value of 10 or more is a delay in milliseconds.

Returns Plugin The plugin.

Using toggle()
plugin.toggle('open');
console.log(plugin.get('open'));  // true
method

plugin.upd([path], [type])

Notifies the listeners of a path.
ParameterTypeDescription
path? String The relative path.
type? Number The type of the change. A value of 10 or more is a delay in milliseconds.

Returns Plugin The plugin.

Using upd()
plugin.watch('name', value => console.log(value));  // Peter
window.users.name = 'Peter';
plugin.upd('name');
method

plugin.reset([path])

Clears the state of components bound to a path.
ParameterTypeDescription
path? String The relative path.

Returns Plugin The plugin.

The touched and modified state is cleared. The value does not change.

Using reset()
var com = FIND('field');
com.modify('Peter');
plugin.reset();
console.log(com.config.touched, GET('users.name'));  // false Peter
method

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

Watches a path of the plugin.
ParameterTypeDescription
path String The relative path.
callback Function The function that receives the changes.
init? Boolean true calls the callback immediately.

Returns Plugin The plugin.

The callback receives value, path and type. Several paths are separated by a comma or + .

Using watch()
plugin.watch('name', (value, path) => console.log(value, path));  // Peter users.name
plugin.set('name', 'Peter');
method

plugin.on(name, callback)

Listens for a global event.
ParameterTypeDescription
name String The name of the event.
callback Function The listener.

Returns Plugin The plugin.

The scope of the plugin is active when the listener is registered.

Using on()
plugin.on('saved', value => console.log(value));  // 5
EMIT('saved', 5);
method

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

Calls a method of the plugin.
ParameterTypeDescription
name String The name of the method.

Returns Plugin The plugin.

An unknown method is ignored. The method accepts up to five arguments.

Using exec()
plugin.hello = name => console.log('Hello ' + name);  // Hello Peter
plugin.exec('hello', 'Peter');
method

plugin.makepath([path])

Resolves a path relative to the plugin.
ParameterTypeDescription
path? String The path.

Returns String The resolved path.

The forms are:

ArgumentResult for the users plugin
omittedusers
name or ?.nameusers.name
*namecommon.name
@flagusers @flag
^name^users.name
+name+users.name
|nameusers|name
%name%name
#name#name
Using makepath()
console.log(plugin.makepath('name'));  // users.name
method

plugin.format([text])

Inserts the name of the plugin into a text.
ParameterTypeDescription
text? String The text.

Returns String The result.

The name replaces {0}. A text without a placeholder receives the name at the end.

Using format()
console.log(plugin.format('/api/{0}/'));  // /api/users/
method

plugin.scope([path])

Makes the plugin the active scope.
ParameterTypeDescription
path? String The scope to activate.

Returns Plugin The plugin.

Other functions then resolve ? to this plugin. Another path can be passed, and null clears the active scope. The method is also available as plugin().

Using scope()
plugin.scope();
console.log(M.scope());  // users
method

plugin.ajax(url, [data], [callback])

Sends a request.
ParameterTypeDescription
url String The method and the URL.
data? Object The data of the request.
callback? Function|String A function or a relative path.

Returns Plugin|Promise The plugin, or a Promise without a callback.

The method calls AJAX(). A text callback is a path relative to the plugin, which receives the response. Without data and a callback, the method returns a Promise.

Using ajax()
plugin.ajax('GET /api/users/', 'response');
console.log(plugin.get('response.url'));  // GET /api/users/
method

plugin.api(url, [data], [callback])

Calls an API endpoint.
ParameterTypeDescription
url String The name of the endpoint.
data? Object The data of the request.
callback? Function|String A function or a relative path.

Returns Plugin|Promise The plugin, or a Promise without a callback.

The method calls API() and accepts the same callback forms as ajax().

Using api()
plugin.api('users_read', function(response) {
    console.log(response);  // users_read
});
method

plugin.dapi(name, [data], [callback])

Calls a DAPI endpoint.
ParameterTypeDescription
name String The name of the endpoint.
data? Object The data of the request.
callback? Function|String A function or a relative path.

Returns Plugin|Promise The plugin, or a Promise without a callback.

The method calls DAPI() and accepts the same callback forms as ajax().

Using dapi()
plugin.dapi('users', function(response) {
    console.log(response);  // users
});
method

plugin.tapi(name, [data], [callback])

Calls a TAPI endpoint.
ParameterTypeDescription
name String The name of the endpoint.
data? Object The data of the request.
callback? Function|String A function or a relative path.

Returns Plugin|Promise The plugin, or a Promise without a callback.

The method calls TAPI() and accepts the same callback forms as ajax().

Using tapi()
plugin.tapi('users', function(response) {
    console.log(response);  // users
});
method

plugin.upload(url, data, [callback])

Uploads data.
ParameterTypeDescription
url String The URL.
data FormData|Object The data.
callback? Function|String A function or a relative path.

Returns Plugin|Promise The plugin, or a Promise without a callback.

The method calls UPLOAD() and accepts the same callback forms as ajax().

Using upload()
plugin.upload('/upload/', new FormData(), function(response) {
    console.log(response);  // /upload/
});
method

plugin.remove()

Removes the plugin.

Returns Boolean Always true.

The destroy delegate runs, and the events and watchers registered during the declaration are removed. The element stays in the page.

Using remove()
console.log(plugin.remove(), PLUGINS.users === undefined);  // true true
Made in Europe