Plugins
Organise the logic and data of a part of the application with PLUGIN() and ui-plugin.
On this page
A plugin holds the data and the logic of one part of an application, such as a page, a form or a panel. It owns a path with the same name as the plugin, so the plugin orders keeps its data in orders.*. The HTML connects to it with the <ui-plugin> element.
Declaring a plugin
PLUGIN(name, declaration) creates a plugin. The declaration receives the plugin instance, usually named exports. Everything you assign to it becomes a public method:
<script>
PLUGIN('counter', function(exports) {
exports.set('count', 0);
exports.increment = function() {
exports.inc('count', 1);
};
});
</script>
<ui-plugin path="counter">
<b><ui-bind path="?.count" config="text"></ui-bind></b>
<button class="exec" data-exec="?/increment">+1</button>
</ui-plugin>Inside <ui-plugin>, the ? character stands for the plugin path, so ?.count is counter.count and ?/increment is the method increment of the plugin. An element with the class exec calls the method named in data-exec when it is clicked.
The name * declares a plugin on the common path, whose name comes from DEF.pathcommon (common by default).
The ui-plugin element
<ui-plugin> connects a part of the page with a plugin. Its config attribute holds settings, and its default attribute holds a JavaScript expression that becomes the plugin model:
<script>
PLUGIN('settings', function(exports) {
exports.show = function() {
var config = exports.element.scope().config;
console.log(config.title, config.size);
console.log(exports.model);
};
});
</script>
<ui-plugin path="settings" config="title:Account;size:10" default="{ theme: 'light' }"></ui-plugin>PLUGINS.settings.show() prints Account 10 and { theme: 'light' }.
exports.element is the <ui-plugin> element once the plugin is connected to it. While a declaration placed ahead of the element runs, exports.element is still the <body>. If no plugin is declared for the path, the library creates an empty one. All attributes are in the ui-plugin reference.
Working with data
The methods of a plugin work with paths relative to the plugin. Without a path, they work with the whole model:
| Method | Description |
|---|---|
exports.get([path]) | Reads a value. |
exports.set([path], value, [type]) | Writes a value. |
exports.push([path], value, [type]) | Adds an item to an array. |
exports.inc([path], value, [type]) | Increases a number. |
exports.extend([path], value, [type]) | Merges an object into an existing object. |
exports.toggle(path, [type]) | Inverts a boolean value. |
exports.nul(path, [type]) | Writes null. |
exports.upd(path, [type]) | Notifies bindings about a change you made directly. |
exports.reset([path]) | Resets the state of the form fields under the path. |
exports.makepath([path]) | Returns the absolute path. |
PLUGIN('todo', function(exports) {
exports.set({ items: [], done: false, total: 1 });
exports.push('items', 'Write docs');
exports.inc('total', 2);
exports.toggle('done');
exports.get('total'); // 3
exports.model.items; // ['Write docs']
exports.makepath('items'); // 'todo.items'
});Four properties read the model directly:
| Property | Description |
|---|---|
exports.model | Reads or replaces the whole model. |
exports.data | The same as exports.model. |
exports.form | Returns the model and resets the state of its form fields. |
exports.modified | Returns only the fields the user changed and resets the state of the form fields. |
Forms and validation shows exports.form and exports.modified in use.
Calling a plugin
Use EXEC() with the plugin name, a slash and the method name. PLUGINS holds all running plugins by name:
EXEC('counter/increment');
PLUGINS.counter.increment();exports.exec('method', [arguments]) calls a method of the same plugin. When one plugin calls another through EXEC(), the called plugin finds the calling plugin in exports.caller:
<script>
PLUGIN('page', function(exports) {
exports.refresh = function() {
console.log('refreshed by', exports.caller.name);
};
});
PLUGIN('page.toolbar', function(exports) {
exports.reload = function() {
EXEC('page/refresh');
};
});
</script>
<ui-plugin path="page">
<ui-plugin path="toolbar"></ui-plugin>
</ui-plugin>EXEC('page.toolbar/reload') prints refreshed by page.toolbar.
Nested plugins
A <ui-plugin> inside another one extends the path of its parent. In the example above, the inner element uses the plugin page.toolbar, and PLUGINS['page.toolbar'].parent returns the plugin page. Scopes describes how nested paths are resolved.
Watching values and events
exports.watch() observes a path relative to the plugin. exports.on() listens to a global event. Both stop automatically when the plugin is removed:
PLUGIN('clock', function(exports) {
exports.watch('time', function(value, path, type) {
console.log('watch', value, path);
});
exports.on('tick', function(value) {
exports.set('time', value);
});
exports.destroy = function() {
console.log('destroyed');
};
});EMIT('tick', 1) prints watch 1 clock.time. The callback of exports.watch() receives the value first and the path second.
One declaration for many instances
A name with a space declares a shared definition. The word after the space is the name of the definition, and each <ui-plugin> that uses it gets its own instance on its own path:
<script>
PLUGIN('? panel', function(exports) {
exports.set('title', 'Panel ' + exports.name);
});
</script>
<ui-plugin path="left panel">
<ui-bind path="?.title" config="text"></ui-bind>
</ui-plugin>
<ui-plugin path="right panel">
<ui-bind path="?.title" config="text"></ui-bind>
</ui-plugin>This page runs the plugins left and right, which show Panel left and Panel right.
Plugins created on demand
PLUGINABLE(name, declaration) registers a plugin without running it. The plugin is created by the first EXEC() that calls it:
PLUGINABLE('report', function(exports) {
exports.build = function(year) {
console.log('building', year);
};
});
PLUGINS.report; // undefined
EXEC('report/build', 2024); // creates the plugin and calls build()Calling the server
exports.ajax() sends a request. The callback can be a function, a path relative to the plugin, or missing. Without a callback and without data, the method returns a promise:
PLUGIN('users', function(exports) {
exports.load = function() {
// A function
exports.ajax('GET /api/users/', function(response) {
console.log(response);
});
// A path: the response is stored in "users.saved"
exports.ajax('POST /api/users/', { name: 'Peter' }, 'saved');
};
exports.read = async function() {
var response = await exports.ajax('GET /api/users/1/');
console.log(response);
};
});exports.api(), exports.tapi(), exports.dapi() and exports.upload() take the same arguments and use the matching global function. See the Plugin reference.
Removing a plugin
exports.remove() emits the plugin.destroy event, runs the exports.destroy() delegate, stops the watchers and events of the plugin and deletes it from PLUGINS. Declaring a plugin with a name that is already in use removes the running plugin and replaces it:
PLUGIN('editor', function(exports) {
exports.destroy = function() {
console.log('first instance destroyed');
};
});
PLUGIN('editor', function(exports) {
exports.version = 2;
});