Spanish Total.js
English
Documentation For AI agents
Contents

Macros

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

.md GitHub
On this page

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 or a ui-bind command is not enough and a full component 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:

ArgumentTypeDescription
selfMacroThe macro instance.
elementjQueryThe element of the binding.
clsStringA 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:

<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:

<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.

DelegateWhen 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

MemberDescription
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.pathThe path of the binding.
self.nameThe macro name.
self.elementThe element as a jQuery object.
self.domThe DOM element.
self.binderThe 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 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:

<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 and Plugins.

Cleaning up

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

<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.

Made in Europe