Plugin
The properties, delegates and methods of a plugin instance.
On this page
API
delegate
exports.destroy = function()
Runs when the plugin is removed.
exports.destroy = function()<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.
plugin.nameconsole.log(plugin.name); // users
property
plugin.id
The identifier of the plugin.
plugin.idIt is the name with the plug prefix.
console.log(plugin.id); // plugusers
property
plugin.element
The element of the plugin as a jQuery object.
plugin.elementIt is the element of the scope. A plugin without a scope uses body.
console.log(plugin.element.prop('tagName')); // UI-PLUGIN
property
plugin.model
Reads or replaces the whole model of the plugin.
plugin.modelReading a missing model returns an empty object.
plugin.model = { name: 'Peter' };
console.log(plugin.model.name); // Peter
property
plugin.data
Reads or replaces the whole model of the plugin.
plugin.dataIt is the same as model.
plugin.data = { name: 'Anna' };
console.log(plugin.data.name); // Anna
property
plugin.form
Returns the model and resets the state of its components.
plugin.formReading the property clears the touched and modified state of the components bound to the model.
plugin.set('name', 'Peter');
console.log(plugin.form); // {"name":"Peter"}
property
plugin.modified
Returns the fields changed by components.
plugin.modifiedReading the property clears the touched and modified state of the components bound to the model.
FIND('field').modify('Peter');
console.log(plugin.modified); // {"name":"Peter"}
property
plugin.parent
The plugin of the parent scope.
plugin.parentThe value is null when there is no parent scope.
console.log(PLUGINS['users.detail'].parent.name); // users
property
plugin.caller
The plugin that was active when this plugin was created.
plugin.callerThe value is null when no plugin was active.
console.log(plugin.caller); // null
method
plugin.set([path], value, [type])
Writes a value to a path of the plugin.
plugin.set([path], value, [type])| Parameter | Type | Description |
|---|---|---|
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.
plugin.set('name', 'Peter');
console.log(GET('users.name')); // Peter
method
plugin.get([path])
Reads a path of the plugin.
plugin.get([path])| Parameter | Type | Description |
|---|---|---|
path? |
String |
The relative path. |
Returns Object The value.
Without an argument, the method returns the whole model.
plugin.set('name', 'Peter');
console.log(plugin.get('name')); // Peter
method
plugin.nul(path, [type])
Writes null to a path of the plugin.
plugin.nul(path, [type])| Parameter | Type | Description |
|---|---|---|
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.
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.
plugin.push([path], value, [type])| Parameter | Type | Description |
|---|---|---|
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.
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.
plugin.extend([path], value, [type])| Parameter | Type | Description |
|---|---|---|
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.
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.
plugin.inc([path], value, [type])| Parameter | Type | Description |
|---|---|---|
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.
plugin.inc('count', 2);
plugin.inc('count', 3);
console.log(plugin.get('count')); // 5
method
plugin.toggle(path, [type])
Inverts a boolean value.
plugin.toggle(path, [type])| Parameter | Type | Description |
|---|---|---|
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.
plugin.toggle('open');
console.log(plugin.get('open')); // true
method
plugin.upd([path], [type])
Notifies the listeners of a path.
plugin.upd([path], [type])| Parameter | Type | Description |
|---|---|---|
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.
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.
plugin.reset([path])| Parameter | Type | Description |
|---|---|---|
path? |
String |
The relative path. |
Returns Plugin The plugin.
The touched and modified state is cleared. The value does not change.
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.
plugin.watch(path, callback, [init])| Parameter | Type | Description |
|---|---|---|
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 + .
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.
plugin.on(name, callback)| Parameter | Type | Description |
|---|---|---|
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.
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.
plugin.exec(name, [a], [b], [c], [d], [e])| Parameter | Type | Description |
|---|---|---|
name |
String |
The name of the method. |
Returns Plugin The plugin.
An unknown method is ignored. The method accepts up to five arguments.
plugin.hello = name => console.log('Hello ' + name); // Hello Peter
plugin.exec('hello', 'Peter');
method
plugin.makepath([path])
Resolves a path relative to the plugin.
plugin.makepath([path])| Parameter | Type | Description |
|---|---|---|
path? |
String |
The path. |
Returns String The resolved path.
The forms are:
| Argument | Result for the users plugin |
|---|---|
| omitted | users |
name or ?.name | users.name |
*name | common.name |
@flag | users @flag |
^name | ^users.name |
+name | +users.name |
|name | users|name |
%name | %name |
#name | #name |
console.log(plugin.makepath('name')); // users.name
method
plugin.format([text])
Inserts the name of the plugin into a text.
plugin.format([text])| Parameter | Type | Description |
|---|---|---|
text? |
String |
The text. |
Returns String The result.
The name replaces {0}. A text without a placeholder receives the name at the end.
console.log(plugin.format('/api/{0}/')); // /api/users/
method
plugin.scope([path])
Makes the plugin the active scope.
plugin.scope([path])| Parameter | Type | Description |
|---|---|---|
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().
plugin.scope();
console.log(M.scope()); // users
method
plugin.ajax(url, [data], [callback])
Sends a request.
plugin.ajax(url, [data], [callback])| Parameter | Type | Description |
|---|---|---|
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.
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.
plugin.api(url, [data], [callback])| Parameter | Type | Description |
|---|---|---|
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().
plugin.api('users_read', function(response) {
console.log(response); // users_read
});
method
plugin.dapi(name, [data], [callback])
Calls a DAPI endpoint.
plugin.dapi(name, [data], [callback])| Parameter | Type | Description |
|---|---|---|
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().
plugin.dapi('users', function(response) {
console.log(response); // users
});
method
plugin.tapi(name, [data], [callback])
Calls a TAPI endpoint.
plugin.tapi(name, [data], [callback])| Parameter | Type | Description |
|---|---|---|
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().
plugin.tapi('users', function(response) {
console.log(response); // users
});
method
plugin.upload(url, data, [callback])
Uploads data.
plugin.upload(url, data, [callback])| Parameter | Type | Description |
|---|---|---|
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().
plugin.upload('/upload/', new FormData(), function(response) {
console.log(response); // /upload/
});
method
plugin.remove()
Removes the plugin.
plugin.remove()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.
console.log(plugin.remove(), PLUGINS.users === undefined); // true true