Spanish Total.js
English
Documentation For AI agents
Contents

Macro

The delegates, properties and methods of a macro instance.

.md GitHub
On this page

A macro instance is the object passed to the declaration of MACRO(). It adds behaviour to an element with the macro command of <ui-bind>. Macros explains the concept. In the examples, macro is an instance attached to a <ui-bind> bound to form.name.

API

delegate

self.make = function()

Runs once after the declaration.
Preparing the element
<ui-bind path="form.name" config="macro:card"></ui-bind>

<script>
    MACRO('card', function(self, element, cls) {
        self.make = function() {
            element.aclass(cls);
        };
    });
</script>
delegate

self.setter = function(value, path, type)

Receives the value of the path.
ParameterTypeDescription
value Object The value of the path.
path String The path that changed.
type Number The type of the change.

The delegate runs on each update of the binder.

Rendering the value
<ui-bind path="form.name" config="macro:card"></ui-bind>

<script>
    MACRO('card', function(self) {
        self.setter = function(value) {
            self.text(value);
        };
    });
    SET('form.name', 'Peter');
</script>
delegate

self.service = function(counter)

Runs once per minute.
ParameterTypeDescription
counter Number The number of runs.

The delegate receives a counter that grows by one on each run.

delegate

self.destroy = function()

Runs when the element of the binder is removed.
Releasing resources
<ui-bind path="form.name" config="macro:card"></ui-bind>

<script>
    MACRO('card', function(self) {
        self.destroy = () => console.log('destroyed');  // destroyed
    });
    setTimeout(function() {
        $('ui-bind').remove();
        FREE();
    }, 500);
</script>
property

self.name

The name of the macro.
Reading name
console.log(macro.name);  // demo
property

self.path

The path of the binder.
Reading path
console.log(macro.path);  // form.name
property

self.element

The element of the binder as a jQuery object.
Reading element
console.log(macro.element.attr('id'));  // box
property

self.dom

The element of the binder as a DOM node.
Reading dom
console.log(macro.dom.id);  // box
property

self.binder

The binder that created the macro.

See the refresh() and exec() methods.

Reading binder
console.log(macro.binder.path);  // form.name
property

self.scope

The Scope of the binder.

The property is assigned after the make delegate and exists when the path contains ?.

Reading scope
console.log(macro.scope.path);  // users
method

self.get()

Reads the value of the path.

Returns Object The value.

Using get()
SET('form.name', 'Peter');
console.log(macro.get());  // Peter
method

self.set(value)

Writes a value to the path.
ParameterTypeDescription
value Object The new value.
Using set()
macro.set('Peter');
console.log(GET('form.name'));  // Peter
method

self.refresh()

Runs the binder again with the current value.

Returns Macro The macro.

Using refresh()
macro.setter = value => console.log(value);  // Peter
window.form.name = 'Peter';
macro.refresh();
method

self.EXEC(path, [a], [b], [c], [d])

Calls a function by its path.
ParameterTypeDescription
path String The path of the function.

Returns Macro The macro.

The ? character is replaced with the path of the scope. The @ prefix calls a method of the parent component.

Using EXEC()
macro.EXEC('@ping', 5);
method

self.SEEX(path, [a], [b], [c], [d])

Calls a function or writes a value.
ParameterTypeDescription
path String The path.

Returns Macro The macro.

When the path holds no function, the first argument is written to it.

Using SEEX()
macro.SEEX('@title', 'Orders');
console.log(FIND('owner').title);  // Orders
method

self.find(selector)

Finds nested elements.
ParameterTypeDescription
selector String|Object A selector or an object of selectors.

Returns jQuery|Object The result.

An object of selectors returns an object of jQuery results with the same keys.

Using find()
console.log(macro.find('.label').text());  // Text
method

self.closest(selector)

Finds the closest matching parent.
ParameterTypeDescription
selector String The selector.

Returns jQuery The result.

Using closest()
console.log(macro.closest('body').length);  // 1
method

self.parent([selector])

Returns a parent element.
ParameterTypeDescription
selector? String The selector.

Returns jQuery The result.

The argument accepts the same values as parent() of a component.

Using parent()
console.log(macro.parent().prop('tagName'));  // BODY
method

self.html([value])

Reads or replaces the HTML content.
ParameterTypeDescription
value? String|Array The new content.

Returns String|jQuery The content, or the element when value is passed.

An array is joined.

Using html()
macro.html('<i>A</i>');
console.log(macro.html());  // <i>A</i>
method

self.text([value])

Reads or replaces the text content.
ParameterTypeDescription
value? String|Array The new content.

Returns String|jQuery The content, or the element when value is passed.

An array is joined.

Using text()
macro.text('A');
console.log(macro.text());  // A
method

self.append(value)

Adds content to the end of the element.
ParameterTypeDescription
value String|Array The content.

Returns jQuery The element.

An array is joined.

Using append()
macro.append('<i>A</i>');
console.log(macro.find('i').length);  // 1
method

self.empty()

Removes the content of the element.

Returns jQuery The element.

Using empty()
macro.empty();
console.log(macro.html() === '');  // true
method

self.event(name, [selector], callback)

Attaches a jQuery event to the element.
ParameterTypeDescription
name String The name of the event.
selector? String The delegated selector.
callback Function The handler.

Returns Macro The macro.

The arguments are the same as in the jQuery on() method.

Using event()
macro.event('click', '.label', () => console.log('clicked'));  // clicked
macro.find('.label').click();
method

self.aclass(classes, [timeout])

Adds CSS classes.
ParameterTypeDescription
classes String The classes.
timeout? Number The delay in milliseconds.

Returns Macro The macro.

Using aclass()
macro.aclass('active');
console.log(macro.hclass('active'));  // true
method

self.rclass([classes], [timeout])

Removes CSS classes.
ParameterTypeDescription
classes? String The classes.
timeout? Number The delay in milliseconds.

Returns Macro The macro.

Without an argument, all classes are removed.

Using rclass()
macro.aclass('active');
macro.rclass('active');
console.log(macro.hclass('active'));  // false
method

self.rclass2(search)

Removes CSS classes that contain a text.
ParameterTypeDescription
search String|RegExp The text or a regular expression.

Returns Macro The macro.

Using rclass2()
macro.aclass('col-1 col-2');
macro.rclass2('col-');
console.log(macro.hclass('col-2'));  // false
method

self.tclass(classes, [state])

Toggles CSS classes.
ParameterTypeDescription
classes String The classes.
state? Boolean Forces the state.

Returns Macro The macro.

Using tclass()
macro.tclass('active', true);
console.log(macro.hclass('active'));  // true
method

self.hclass(name)

Tells whether the element has a CSS class.
ParameterTypeDescription
name String The class.

Returns Boolean The result.

Using hclass()
console.log(macro.hclass('missing'));  // false
method

self.attr(name, [value])

Reads or writes an attribute.
ParameterTypeDescription
name String The attribute.
value? String The new value.

Returns String|Macro The value, or the macro when value is passed.

Using attr()
macro.attr('title', 'Orders');
console.log(macro.attr('title'));  // Orders
method

self.attrd(name, [value])

Reads or writes a data- attribute.
ParameterTypeDescription
name String The name without the data- prefix.
value? String The new value.

Returns String|Macro The value, or the macro when value is passed.

Using attrd()
macro.attrd('id', '5');
console.log(macro.attr('data-id'));  // 5
method

self.attrd2(name)

Reads a data- attribute from the element or its closest parent that has it.
ParameterTypeDescription
name String The name without the data- prefix.

Returns String The value.

Using attrd2()
console.log(macro.attrd2('area'));  // main
method

self.css(name, [value])

Reads or writes a CSS property.
ParameterTypeDescription
name String The property.
value? String The new value.

Returns String|Macro The value, or the macro when value is passed.

Using css()
macro.css('display', 'block');
console.log(macro.css('display'));  // block
method

self.visible([callback])

Tells whether the element is visible.
ParameterTypeDescription
callback? Function Runs when the element is visible.

Returns Boolean The current visibility.

With a callback, the function runs as soon as the element is visible.

Using visible()
console.log(macro.visible());  // true
method

self.width([callback])

Returns the width of the element in pixels.
ParameterTypeDescription
callback? Function Receives the width.

Returns Number The width.

With a callback, the function runs as soon as the width is greater than zero.

Using width()
macro.css('display', 'block');
macro.css('width', '200px');
console.log(macro.width());  // 200
method

self.height([callback])

Returns the height of the element in pixels.
ParameterTypeDescription
callback? Function Receives the height.

Returns Number The height.

With a callback, the function runs as soon as the height is greater than zero.

Using height()
macro.css('display', 'block');
macro.css('height', '50px');
console.log(macro.height());  // 50
method

self.autofocus([selector])

Moves the focus to the first visible field.
ParameterTypeDescription
selector? String The fields. Default: input,select,textarea

Returns Macro The macro.

The method does nothing on mobile devices.

Using autofocus()
macro.autofocus();
console.log(document.activeElement.tagName);  // INPUT
method

self.faicon(value)

Completes the name of an icon.
ParameterTypeDescription
value String The name of the icon.

Returns String The CSS classes.

A name without a space receives DEF.iconprefix, which is ti ti-.

Using faicon()
console.log(macro.faicon('home'));  // ti ti-home
Made in Europe