Plugin
The properties, the methods and the delegates of a plugin instance.
On this page
API
property
name
The path of the plugin.
nameReturns String The path of the plugin.
property
path
The path of the plugin model.
pathReturns Path The path of the plugin model.
See Path.
property
config
The configuration of the plugin.
configReturns Object The configuration of the plugin.
property
element
The element of the plugin as a jQuery object.
elementReturns jQuery The element of the plugin as a jQuery object.
property
dom
The element of the plugin.
domReturns HTMLElement The element of the plugin.
property
scope
The object that paths are resolved against, which is window.
scopeReturns Object The object that paths are resolved against, which is window.
property
cls
The value of the path attribute of the element.
clsReturns String The value of the path attribute of the element.
property
ready
Is true when the plugin is initialized.
readyReturns Boolean Is true when the plugin is initialized.
property
plugin
The parent plugin of a plugin nested with a ? in its path, otherwise null.
pluginReturns Plugin The parent plugin of a plugin nested with a ? in its path, otherwise null.
property
parent
The same value as the plugin property.
parentReturns Plugin The same value as the plugin property.
<ui-plugin path="orders">
<ui-plugin path="?.detail"></ui-plugin>
</ui-plugin>
<script>
PLUGIN('orders', function(exports) {});
PLUGIN('orders.detail', function(exports) {
exports.make = () => console.log(exports.name, exports.parent.name);
});
// logs: orders.detail orders
</script>
property
caller
The plugin that called a method of this plugin through EXEC().
callerReturns Plugin The plugin that called a method of this plugin through EXEC().
See talker().
property
model
Reads or replaces the model of the plugin.
modelReturns Object Reads or replaces the model of the plugin.
Assigning a value does not notify anything bound to the path. Use set() or upd() for that.
plugin.set('count', 2);
console.log(JSON.stringify(plugin.model));
// logs: {"count":2}
plugin.model = { count: 3 };
console.log(orders.count);
// logs: 3
property
modified
Returns the model and clears the state of its components.
modifiedReturns Object The model of the plugin.
Reading the property returns the whole model, like get(), and clears the touched and modified state of the components in the plugin, like the flag @reset. Read it after saving a form.
<ui-plugin path="orders">
<ui-component name="field" path="?.city"></ui-component>
</ui-plugin>
<script>
COMPONENT('field', function(self) {});
PLUGIN('orders', function(exports) {
exports.save = function() {
console.log(JSON.stringify(exports.modified));
console.log(FIND('field').config.modified);
};
});
ON('ready', function() {
SET('orders.city @change', 'Prague');
EXEC('orders/save');
});
// logs: {"city":"Prague"}
// logs: 0
</script>
property
data
The same value as the model property.
dataReturns Object The same value as the model property.
property
form
Returns the model and clears the touched and modified state of the components in the path of the plugin.
formReturns Object Returns the model and clears the touched and modified state of the components in the path of the plugin.
<ui-plugin path="orders">
<ui-component name="field" path="?.name"></ui-component>
</ui-plugin>
<script>
PLUGIN('orders', function(exports) {
window.plugin = exports;
});
COMPONENT('field', function(self) {
window.com = self;
});
com.modify('Anna');
console.log(com.element.attr('class'));
// logs: ui-touched ui-modified
console.log(plugin.form.name, com.element.hclass('ui-touched'));
// logs: Anna false
</script>
delegate
make()
Runs when the plugin is created.
make()<ui-plugin path="orders"></ui-plugin>
<script>
PLUGIN('orders', function(exports) {
exports.make = () => console.log(exports.path.toString());
});
// logs: orders
</script>
delegate
destroy()
Runs when the plugin is removed.
destroy()The regular cleanup removes the plugins whose element left the page.
<ui-plugin path="orders"></ui-plugin>
<script>
PLUGIN('orders', function(exports) {
exports.destroy = () => console.log('destroyed');
});
$('ui-plugin').remove();
jComponent.free();
// logs: destroyed
</script>
method
get([path])
Reads a value from the model.
get([path])| Parameter | Type | Description |
|---|---|---|
path? |
String |
A path relative to the plugin. |
Returns Object The value.
Without a path, the method returns the whole model. A path with the @reset flag also clears the state of the components in the path.
plugin.set('user.name', 'Anna');
console.log(plugin.get('user.name'));
// logs: Anna
method
set([path], value)
Writes a value to the model and notifies everything bound to the path.
set([path], value)| Parameter | Type | Description |
|---|---|---|
path |
String |
A path relative to the plugin. |
value |
Object |
The value to write. |
With an object as the only argument, the method replaces the whole model.
plugin.set('status', 'open');
console.log(orders.status);
// logs: open
plugin.set({ status: 'new', items: [] });
console.log(JSON.stringify(orders));
// logs: {"status":"new","items":[]}
method
push(path, value)
Adds a value to an array in the model.
push(path, value)| Parameter | Type | Description |
|---|---|---|
path |
String |
A path relative to the plugin. |
value |
Object |
The value to write. |
Each item of an array value is added separately.
plugin.push('items', 1);
plugin.push('items', [2, 3]);
console.log(orders.items.join());
// logs: 1,2,3
method
inc(path, [value])
Increases a number in the model.
inc(path, [value])| Parameter | Type | Description |
|---|---|---|
path |
String |
A path relative to the plugin. |
value? |
Number |
The increment. Default: 1 |
plugin.inc('count');
plugin.inc('count', 4);
console.log(orders.count);
// logs: 5
method
toggle(path)
Negates a value in the model.
toggle(path)| Parameter | Type | Description |
|---|---|---|
path |
String |
A path relative to the plugin. |
plugin.toggle('open');
console.log(orders.open);
// logs: true
method
nul(path)
Writes null to the model.
nul(path)| Parameter | Type | Description |
|---|---|---|
path |
String |
A path relative to the plugin. |
plugin.set('user', 'Anna');
plugin.nul('user');
console.log(orders.user);
// logs: null
method
upd(path)
Notifies everything bound to a path, without changing the value.
upd(path)| Parameter | Type | Description |
|---|---|---|
path |
String |
A path relative to the plugin. |
The method is also available as update().
plugin.watch('count', value => console.log(value));
orders.count = 5;
plugin.upd('count');
// logs: 5
method
watch(path, callback, [autoinit])
Calls a function on each change at a path of the model.
watch(path, callback, [autoinit])| Parameter | Type | Description |
|---|---|---|
path |
String |
A path relative to the plugin. |
callback |
Function |
A function (value, flags, path). |
autoinit? |
Boolean |
Calls the function right away with the current value. Default: false |
With a function as the first argument, the whole model is observed. Join several paths with +.
plugin.watch('status', (value, flags, path) => console.log(value, path));
plugin.set('status', 'open');
// logs: open orders.status
method
unwatch(path, [callback])
Removes watchers registered with watch().
unwatch(path, [callback])| Parameter | Type | Description |
|---|---|---|
path |
String |
A path relative to the plugin. |
callback? |
Function |
The watcher to remove. |
Without callback, all the watchers of the path are removed. With a function as the only argument, the watcher of the whole model is removed. Join several paths with +.
var log = value => console.log('status', value);
plugin.watch('status', log);
plugin.set('status', 'open');
plugin.unwatch('status', log);
plugin.set('status', 'closed');
// logs: status open
method
makepath(path)
Replaces the ? in a path with the path of the plugin.
makepath(path)| Parameter | Type | Description |
|---|---|---|
path |
String |
The path. |
Returns String The resulting path.
console.log(plugin.makepath('?.items'));
// logs: orders.items
method
cachepath(path, expire, [def])
Keeps a value of the model in the cache between visits.
cachepath(path, expire, [def])| Parameter | Type | Description |
|---|---|---|
path |
String |
A path relative to the plugin. |
expire |
String |
The lifetime of the saved value, for example 1 day. |
def? |
Object|Function |
The value used when nothing is saved. |
The same as the global CACHEPATH(), with a path relative to the plugin.
plugin.cachepath('filter', '1 day', 'all');
console.log(orders.filter);
// logs: all
method
format(value)
Inserts the path of the plugin into a text.
format(value)| Parameter | Type | Description |
|---|---|---|
value |
String |
The text. |
Returns String The resulting text.
The text {0} is replaced with the path. Without {0}, the path is added to the end of the text.
console.log(plugin.format('{0}.items'));
// logs: orders.items
method
exec(name, [a], [b], [c], [d], [e])
Calls a method of this plugin.
exec(name, [a], [b], [c], [d], [e])| Parameter | Type | Description |
|---|---|---|
name |
String |
The method name. |
a? |
Object |
The first argument. |
b? |
Object |
The second argument. |
c? |
Object |
The third argument. |
d? |
Object |
The fourth argument. |
e? |
Object |
The fifth argument. |
The name can carry flags. The method runs without this, so refer to the plugin with exports.
plugin.exec('save', 1);
// logs: save 1
method
cmd(name, [a], [b], [c], [d])
Runs a command on the components inside the plugin element.
cmd(name, [a], [b], [c], [d])| Parameter | Type | Description |
|---|---|---|
name |
String |
The command, optionally followed by flags. |
a? |
Object |
The first argument. |
b? |
Object |
The second argument. |
c? |
Object |
The third argument. |
d? |
Object |
The fourth argument. |
<ui-plugin path="orders">
<ui-component name="list"></ui-component>
</ui-plugin>
<ui-component name="list"></ui-component>
<script>
COMPONENT('list', function(self) {
self.command('clear', () => console.log('clear'));
});
PLUGIN('orders', function(exports) {});
ON('ready', () => PLUGINS.orders.cmd('clear'));
// logs: clear
</script>
method
setter(name, [a], [b], [c], [d])
Calls a method of the components inside the plugin, in the form component/method.
setter(name, [a], [b], [c], [d])| Parameter | Type | Description |
|---|---|---|
name |
String |
The target, for example grid/refresh. |
a? |
Object |
The first argument. |
b? |
Object |
The second argument. |
c? |
Object |
The third argument. |
d? |
Object |
The fourth argument. |
The method is also available as SETTER().
<ui-plugin path="orders">
<ui-component name="grid"></ui-component>
</ui-plugin>
<script>
PLUGIN('orders', function(exports) {
window.plugin = exports;
});
COMPONENT('grid', function(self) {
self.reload = value => console.log('reload', value);
});
plugin.setter('grid/reload', 1);
// logs: reload 1
</script>
method
alias(name)
Registers the plugin under another name in PLUGINS.
alias(name)| Parameter | Type | Description |
|---|---|---|
name |
String |
The other name. |
plugin.alias('o');
console.log(PLUGINS.o === plugin);
// logs: true
method
talker()
Marks this plugin as the caller of the next EXEC() call.
talker()The called plugin finds it in its caller property.
<ui-plugin path="orders"></ui-plugin>
<ui-plugin path="users"></ui-plugin>
<script>
PLUGIN('orders', function(exports) {
window.plugin = exports;
});
PLUGIN('users', function(exports) {
exports.load = () => console.log(exports.caller.name);
});
plugin.talker();
EXEC('users/load');
// logs: orders
</script>
method
on(name, callback)
Registers a handler of a global event.
on(name, callback)| Parameter | Type | Description |
|---|---|---|
name |
String |
The event name. |
callback |
Function |
The handler. |
Join several event names with +. See Events.
var handler = value => console.log(value);
plugin.on('saved', handler);
plugin.emit('saved', 1);
plugin.off('saved', handler);
plugin.emit('saved', 2);
// logs: 1
method
off(name, [callback])
Removes a handler of a global event.
off(name, [callback])| Parameter | Type | Description |
|---|---|---|
name |
String |
The event name. |
callback? |
Function |
The handler to remove. |
Without a callback, all handlers of the event registered on this plugin are removed.
method
emit(name, [a], [b], [c], [d])
Emits a global event.
emit(name, [a], [b], [c], [d])| Parameter | Type | Description |
|---|---|---|
name |
String |
The event name. |
a? |
Object |
The first argument. |
b? |
Object |
The second argument. |
c? |
Object |
The third argument. |
d? |
Object |
The fourth argument. |
method
ajax(url, [data], [callback])
Sends a request.
ajax(url, [data], [callback])| Parameter | Type | Description |
|---|---|---|
url |
String |
The method and the address. |
data? |
Object |
The data to send. |
callback? |
Function |
A function (response), or a path. |
The address has the form METHOD /url. A string as the callback is a path relative to the plugin that receives the response.
plugin.ajax('GET /api/orders/', 'items');
console.log(orders.items.length);
// logs: 2
method
tapi(name, [data], [callback])
Calls an operation of the API at the address in DEF.api.
tapi(name, [data], [callback])| Parameter | Type | Description |
|---|---|---|
name |
String |
The name of the operation. |
data? |
Object |
The data to send. |
callback? |
Function |
A function (response), or a path. |
A string as the callback is a path relative to the plugin that receives the response.
DEF.api = '/api/';
plugin.tapi('orders_list', 'items');
// sends: POST /api/ {"schema":"orders_list"}
method
pipe(name, [data], [callback])
Calls an operation of the API at the address in DEF.pipe.
pipe(name, [data], [callback])| Parameter | Type | Description |
|---|---|---|
name |
String |
The name of the operation. |
data? |
Object |
The data to send. |
callback? |
Function |
A function (response), or a path. |
It works in the same way as tapi().
DEF.pipe = '/pipe/';
plugin.pipe('orders_save', { id: 1 }, response => console.log(response.ok));
// logs: true