Spanish Total.js
English
Documentation For AI agents
Contents

Plugins

How to group the data, the logic and the markup of one part of an application into a plugin.

.md GitHub
On this page

A plugin holds one part of an application: a page, a form, a dialog. It owns a piece of the data model, keeps the logic that works with it, and gives the markup inside its element short relative paths. A plugin has two parts: a declaration made with PLUGIN() and a <ui-plugin> element that creates an instance of it.

Declaring a plugin

PLUGIN(name, [config], callback, [dependencies]) registers a declaration. The path attribute of ui-plugin selects the declaration and sets where the plugin keeps its data.

<ui-plugin path="counter" config="step:2">
  <b><ui-bind path="?.count" config="text"></ui-bind></b>
  <button class="exec" data-exec="?/plus">Add</button>
</ui-plugin>

<script>
PLUGIN('counter', 'step:1;label:Clicks', function(exports, config) {

  exports.make = function() {
    exports.set('count', 0);
    console.log(exports.name, config.step, config.label);
  };

  exports.plus = function() {
    exports.inc('count', config.step);
  };

});
</script>

The console prints counter 2 Clicks, and two clicks on the button leave 4 in counter.count.

The callback receives three arguments:

ArgumentDescription
exportsThe plugin instance. It is also the value of this.
configThe merged configuration.
clsThe value of the path attribute of the element.

The instance exposes name, path, element, dom and config. All its methods are listed in the Plugin reference.

Working with the plugin data

Inside the markup of a plugin, ? stands for the plugin path. ?.count in the plugin counter is the global path counter.count. Read more about paths in Paths.

In the code, the methods of exports take a path relative to the plugin:

PLUGIN('cart', function(exports) {
  exports.make = function() {
    exports.set('title', 'My cart');
    exports.push('items', 'Apple');
    exports.push('items', ['Pear', 'Plum']);
    exports.inc('count', 3);
    exports.toggle('open');
    exports.nul('title');
    console.log(JSON.stringify(exports.model));
    console.log(exports.get('items.length'));
  };
});
{"title":null,"items":["Apple","Pear","Plum"],"count":3,"open":true}
3

exports.model reads or replaces the whole plugin data, and exports.data is the same value. Every write notifies the components, bindings and watchers on that path. exports.upd(path) sends the notification without changing the value.

Methods

Each function assigned to exports is a method that other code can call:

WhereHow to call it
Any scriptEXEC('inbox/clear')
Markup inside the plugin<button class="exec" data-exec="?/clear">
The plugin itselfexports.exec('clear')

A click on an element with the class exec calls the method with the element and the event as arguments. Every instance is also available as PLUGINS.name.

Watching data and listening to events

exports.watch(path, callback, [autoinit]) reacts to changes under the plugin path. exports.on(name, callback) listens to a global event sent with EMIT().

PLUGIN('inbox', function(exports) {

  exports.watch('unread', function(value, flags, path) {
    console.log('watch', path, value);
  });

  exports.on('message', function(text) {
    exports.inc('unread');
    console.log('event', text);
  });

  exports.clear = function() {
    exports.set('unread', 0);
  };
});

EMIT('message', 'Hello');
EXEC('inbox/clear');
SET('inbox.unread', 5);
watch inbox.unread 1
event Hello
watch inbox.unread 0
watch inbox.unread 5

Call EMIT() and EXEC() once the plugin exists, for example in ON('ready', callback).

Configuration

The configuration of the element overrides the configuration of the declaration. Three keys have a special meaning:

KeyDescription
initThe name of a function to call with the plugin element when the plugin starts.
classCSS classes to toggle on the element shortly after the plugin starts.
aclassWhen truthy, adds the plugin name to the element as a CSS class.
<ui-plugin path="panel" config="color:red;init:FUNC.panelready;class:loaded;aclass:1"></ui-plugin>

<script>
FUNC.panelready = function(element) {
  console.log('init', element.prop('tagName'));
};

PLUGIN('panel', 'color:blue;size:10', function(exports, config, cls) {
  console.log(config.color, config.size, cls);
});
</script>
red 10 panel
init UI-PLUGIN

The element ends up with the classes panel loaded.

Lifecycle

  1. The callback of PLUGIN() runs. Assign methods and register watchers here.
  2. exports.make() runs, if defined, when the instance is ready.
  3. exports.destroy() runs, if defined, when the plugin is removed.

The library looks for removed elements once per minute. FREE() runs that clean-up at once:

PLUGIN('chart', function(exports) {
  exports.destroy = function() {
    console.log('destroy');
  };
});

$('#area').empty();
console.log(typeof PLUGINS.chart); // object
FREE();                            // destroy
console.log(typeof PLUGINS.chart); // undefined

One declaration, several instances

When the path attribute has two words, the first is the path of the instance and the second is the name of the declaration. Use ? as the first word to get a generated path.

<ui-plugin path="left timer"></ui-plugin>
<ui-plugin path="right timer"></ui-plugin>
<ui-plugin path="? timer"></ui-plugin>

<script>
PLUGIN('timer', function(exports) {
  exports.make = function() {
    exports.set('value', exports.name.length);
  };
});
</script>

left.value is 4 and right.value is 5. Each instance keeps its own data. The third instance has a unique path that starts with jComponent.data..

The common plugin

The name * is a shortcut for the plugin common, a convenient place for what the whole application shares:

<ui-plugin path="*"></ui-plugin>

<script>
PLUGIN('*', function(exports) {
  exports.hello = function() {
    console.log('hello from', exports.name);
  };
});

EXEC('common/hello'); // hello from common
</script>

Nested plugins

A plugin can live inside another plugin. ? in its path is replaced by the path of the parent, ?1 in its markup points one plugin up, and exports.parent returns the parent instance.

<ui-plugin path="order">
  <ui-plugin path="?.customer">
    <ui-bind path="?.name" config="text"></ui-bind>
    /
    <ui-bind path="?1.number" config="text"></ui-bind>
  </ui-plugin>
</ui-plugin>

<script>
PLUGIN('order', function(exports) {
  exports.set('number', 'A-100');
});

PLUGIN('order.customer', function(exports) {
  exports.make = function() {
    exports.set('name', 'Ann');
    console.log(exports.name, exports.parent.name); // order.customer order
  };
});
</script>

The inner plugin renders Ann / A-100, and the data is {"number":"A-100","customer":{"name":"Ann"}}.

Loading data from a server

exports.ajax(url, [data], [callback]) sends a request. When the last argument is a string, the response is written to that path of the plugin:

PLUGIN('products', function(exports) {
  exports.make = function() {

    // Writes the response to "products.items"
    exports.ajax('GET /api/products/', 'items');

    exports.ajax('GET /api/products/', function(response) {
      console.log(response.length);
    });
  };
});

Dependencies

The last argument of PLUGIN() lists what must be ready before the plugin starts. It is a comma-separated string of URLs or an array of URLs and functions. A function receives a next callback.

var loadmaps = function(next) {
  window.MAPS = { ready: true };
  next();
};

PLUGIN('map', function(exports) {
  console.log(window.MAPS.ready); // true
}, ['/js/geo.js', loadmaps]);

URLs are loaded with IMPORT(). To load a whole plugin from a file, see Importing content.

Made in Europe