---
title: Macros
project: jComponent V19
version: 19.194
section: guides
language: en
license: MIT
url: https://totaljs.es/docs/jcomponent-v19/19-194/guides/macros/
updated: 2026-11-18T13:55:49.124Z
---

# Macros

> Attach small reusable behaviour to a ui-bind element with MACRO().

A macro is a small piece of behaviour that you attach to a `<ui-bind>` element. It has no template and no path of its own: it uses the element and the path of the binding it is attached to. Use a macro when a [virtual binding](../virtual-bindings/) or a [ui-bind command](../../reference/ui-bind/) is not enough and a full [component](../creating-components/) would be too much.

## Declaring a macro

`MACRO(name, declaration)` registers a macro. The declaration runs once for every binding that uses the macro and receives three arguments:

| Argument | Type | Description |
| --- | --- | --- |
| `self` | Macro | The macro instance. |
| `element` | jQuery | The element of the binding. |
| `cls` | String | A CSS class name made of `DEF.prefixcssmacros` (`m-` by default) and the macro name. |

This macro counts clicks and writes the result back to the path:

```html
<script>
    MACRO('clicks', function(self, element, cls) {

        self.make = function() {
            self.aclass(cls);
            element.on('click', function() {
                self.set((self.get() || 0) + 1);
            });
        };

        self.setter = function(value) {
            self.text('Clicked ' + value + ' times');
        };
    });

    var counter = 0;
</script>

<ui-bind path="counter" config="macro:clicks"></ui-bind>
```

After two clicks, `counter` is `2`, the element shows `Clicked 2 times` and it has the class `m-clicks`.

## Attaching a macro

Add the `macro` command to the `config` attribute of `<ui-bind>`. Separate several macros with a comma. They run in the order you list them:

```html
<script>
    MACRO('uppercase', function(self) {
        self.setter = function(value) {
            self.text((value || '').toUpperCase());
        };
    });

    MACRO('length', function(self) {
        self.setter = function(value, path) {
            self.attrd('length', (value || '').length);
        };
    });

    var title = 'hello';
</script>

<ui-bind path="title" config="macro:uppercase,length"></ui-bind>
```

After `SET('title', 'world')`, the element shows `WORLD` and `data-length` is `5`.

A macro has to be registered at the moment the binding is created. A name that is not registered at that moment is ignored without an error.

## Delegates

Assign these functions to `self` inside the declaration. All of them are optional.

| Delegate | When it runs |
| --- | --- |
| `self.make()` | Once, right after the declaration. |
| `self.setter(value, path, type)` | With the current value when the binding starts, and on every change of the path. |
| `self.destroy()` | When the element of the binding is removed and the library cleans it up. |
| `self.service(counter)` | Once a minute. `counter` grows with every call. |

## Reading and writing the value

| Member | Description |
| --- | --- |
| `self.get()` | Returns the value of the path. |
| `self.set(value)` | Writes a value to the path. |
| `self.refresh()` | Runs the binding again with the current value. |
| `self.path` | The path of the binding. |
| `self.name` | The macro name. |
| `self.element` | The element as a jQuery object. |
| `self.dom` | The DOM element. |
| `self.binder` | The binding the macro is attached to. |

A macro also has shortcuts for working with its element, such as `self.aclass()`, `self.attrd()`, `self.find()` and `self.text()`. The [Macro reference](../../reference/macro/) lists all of them.

## Macros inside a plugin

When the binding is inside a `<ui-plugin>`, the `?` character in a path refers to that plugin. `self.EXEC()` understands it, so a macro can call a method of the plugin around it:

```html
<script>
    PLUGIN('cart', function(exports) {
        exports.set('total', 3);
        exports.clear = function(value) {
            exports.set('total', value);
        };
    });

    MACRO('clearbutton', function(self, element) {
        element.on('click', function() {
            self.EXEC('?/clear', 0);
        });
        self.setter = function(value) {
            self.text('Clear ' + value + ' items');
        };
    });
</script>

<ui-plugin path="cart">
    <ui-bind path="?.total" config="macro:clearbutton"></ui-bind>
</ui-plugin>
```

The element shows `Clear 3 items`, and `Clear 0 items` after a click.

The property `self.scope` holds the surrounding scope. It is assigned after `self.make()` has finished, so read it in `self.setter()` or in an event handler. See [Scopes](../scopes/) and [Plugins](../plugins/).

## Cleaning up

Release timers and global handlers in `self.destroy()`:

```html
<script>
    MACRO('ticker', function(self) {

        var timer;

        self.make = function() {
            timer = setInterval(function() {
                // periodic work
            }, 1000);
        };

        self.destroy = function() {
            clearInterval(timer);
        };
    });
</script>
```

The library removes bindings of deleted elements during its regular cleanup. Call `FREE()` to run that cleanup right away.
