---
title: Macro
project: jComponent V19
version: 19.194
section: reference
language: it
license: MIT
url: https://totaljs.es/docs/it/jcomponent-v19/19-194/reference/macro/
updated: 2026-11-18T13:58:29.465Z
---

# Macro

> I delegate, le proprietà e i metodi di un'istanza di macro.

Un'istanza di macro è l'oggetto passato alla dichiarazione di `MACRO()`. Aggiunge un comportamento a un elemento con il comando [`macro`](../ui-bind/#macro) di `<ui-bind>`. [Macro](../../guides/macros/) spiega il concetto. Negli esempi, `macro` è un'istanza collegata a un `<ui-bind>` associato a `form.name`.

## API

### self.make = function()

Type: `delegate`

Viene eseguito una sola volta dopo la dichiarazione.

Example: Preparare l'elemento
```html
<ui-bind path="form.name" config="macro:card"></ui-bind>

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

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

Type: `delegate`

Riceve il valore del path.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `value` | `Object` | yes |  | Il valore del path. |
| `path` | `String` | yes |  | Il path che è cambiato. |
| `type` | `Number` | yes |  | Il tipo della modifica. |

Il delegate viene eseguito a ogni aggiornamento del binder.

Example: Eseguire il rendering del valore
```html
<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>
```

### self.service = function(counter)

Type: `delegate`

Viene eseguito una volta al minuto.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `counter` | `Number` | yes |  | Il numero di esecuzioni. |

Il delegate riceve un contatore che aumenta di uno a ogni esecuzione.

### self.destroy = function()

Type: `delegate`

Viene eseguito quando l'elemento del binder viene rimosso.

Example: Rilasciare le risorse
```html
<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>
```

### self.name

Type: `property`

Il nome della macro.

Example: Lettura di name
```js
console.log(macro.name);  // demo
```

### self.path

Type: `property`

Il path del binder.

Example: Lettura di path
```js
console.log(macro.path);  // form.name
```

### self.element

Type: `property`

L'elemento del binder come oggetto jQuery.

Example: Lettura di element
```js
console.log(macro.element.attr('id'));  // box
```

### self.dom

Type: `property`

L'elemento del binder come nodo DOM.

Example: Lettura di dom
```js
console.log(macro.dom.id);  // box
```

### self.binder

Type: `property`

Il binder che ha creato la macro.

Vedi i metodi [`refresh()`](../ui-bind/#refresh) ed [`exec()`](../ui-bind/#exec).

Example: Lettura di binder
```js
console.log(macro.binder.path);  // form.name
```

### self.scope

Type: `property`

Lo [Scope](../scope/) del binder.

La proprietà viene assegnata dopo il delegate `make` ed esiste quando il path contiene `?`.

Example: Lettura di scope
```js
console.log(macro.scope.path);  // users
```

### self.get()

Type: `method`

Legge il valore del path.

Returns: `Object` Il valore.

Example: Uso di get()
```js
SET('form.name', 'Peter');
console.log(macro.get());  // Peter
```

### self.set(value)

Type: `method`

Scrive un valore nel path.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `value` | `Object` | yes |  | Il nuovo valore. |

Example: Uso di set()
```js
macro.set('Peter');
console.log(GET('form.name'));  // Peter
```

### self.refresh()

Type: `method`

Esegue di nuovo il binder con il valore corrente.

Returns: `Macro` La macro.

Example: Uso di refresh()
```js
macro.setter = value => console.log(value);  // Peter
window.form.name = 'Peter';
macro.refresh();
```

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

Type: `method`

Chiama una funzione tramite il suo path.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `path` | `String` | yes |  | Il path della funzione. |

Returns: `Macro` La macro.

Il carattere `?` viene sostituito con il path dello scope. Il prefisso `@` chiama un metodo del componente padre.

Example: Uso di EXEC()
```js
macro.EXEC('@ping', 5);
```

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

Type: `method`

Chiama una funzione o scrive un valore.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `path` | `String` | yes |  | Il path. |

Returns: `Macro` La macro.

Quando il path non contiene una funzione, vi viene scritto il primo argomento.

Example: Uso di SEEX()
```js
macro.SEEX('@title', 'Orders');
console.log(FIND('owner').title);  // Orders
```

### self.find(selector)

Type: `method`

Trova gli elementi annidati.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `selector` | `String|Object` | yes |  | Un selettore o un oggetto di selettori. |

Returns: `jQuery|Object` Il risultato.

Un oggetto di selettori restituisce un oggetto di risultati jQuery con le stesse chiavi.

Example: Uso di find()
```js
console.log(macro.find('.label').text());  // Text
```

### self.closest(selector)

Type: `method`

Trova il genitore corrispondente più vicino.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `selector` | `String` | yes |  | Il selettore. |

Returns: `jQuery` Il risultato.

Example: Uso di closest()
```js
console.log(macro.closest('body').length);  // 1
```

### self.parent([selector])

Type: `method`

Restituisce un elemento genitore.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `selector` | `String` | no |  | Il selettore. |

Returns: `jQuery` Il risultato.

L'argomento accetta gli stessi valori di [`parent()`](../component/#parent) di un componente.

Example: Uso di parent()
```js
console.log(macro.parent().prop('tagName'));  // BODY
```

### self.html([value])

Type: `method`

Legge o sostituisce il contenuto HTML.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `value` | `String|Array` | no |  | Il nuovo contenuto. |

Returns: `String|jQuery` Il contenuto, oppure l'elemento quando passi `value`.

Un array viene unito.

Example: Uso di html()
```js
macro.html('<i>A</i>');
console.log(macro.html());  // <i>A</i>
```

### self.text([value])

Type: `method`

Legge o sostituisce il contenuto testuale.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `value` | `String|Array` | no |  | Il nuovo contenuto. |

Returns: `String|jQuery` Il contenuto, oppure l'elemento quando passi `value`.

Un array viene unito.

Example: Uso di text()
```js
macro.text('A');
console.log(macro.text());  // A
```

### self.append(value)

Type: `method`

Aggiunge contenuto alla fine dell'elemento.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `value` | `String|Array` | yes |  | Il contenuto. |

Returns: `jQuery` L'elemento.

Un array viene unito.

Example: Uso di append()
```js
macro.append('<i>A</i>');
console.log(macro.find('i').length);  // 1
```

### self.empty()

Type: `method`

Rimuove il contenuto dell'elemento.

Returns: `jQuery` L'elemento.

Example: Uso di empty()
```js
macro.empty();
console.log(macro.html() === '');  // true
```

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

Type: `method`

Collega un evento jQuery all'elemento.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | Il nome dell'evento. |
| `selector` | `String` | no |  | Il selettore delegato. |
| `callback` | `Function` | yes |  | Il gestore. |

Returns: `Macro` La macro.

Gli argomenti sono gli stessi del metodo jQuery `on()`.

Example: Uso di event()
```js
macro.event('click', '.label', () => console.log('clicked'));  // clicked
macro.find('.label').click();
```

### self.aclass(classes, [timeout])

Type: `method`

Aggiunge classi CSS.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `classes` | `String` | yes |  | Le classi. |
| `timeout` | `Number` | no |  | Il ritardo in millisecondi. |

Returns: `Macro` La macro.

Example: Uso di aclass()
```js
macro.aclass('active');
console.log(macro.hclass('active'));  // true
```

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

Type: `method`

Rimuove classi CSS.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `classes` | `String` | no |  | Le classi. |
| `timeout` | `Number` | no |  | Il ritardo in millisecondi. |

Returns: `Macro` La macro.

Senza argomenti, vengono rimosse tutte le classi.

Example: Uso di rclass()
```js
macro.aclass('active');
macro.rclass('active');
console.log(macro.hclass('active'));  // false
```

### self.rclass2(search)

Type: `method`

Rimuove le classi CSS che contengono un testo.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `search` | `String|RegExp` | yes |  | Il testo o un'espressione regolare. |

Returns: `Macro` La macro.

Example: Uso di rclass2()
```js
macro.aclass('col-1 col-2');
macro.rclass2('col-');
console.log(macro.hclass('col-2'));  // false
```

### self.tclass(classes, [state])

Type: `method`

Attiva o disattiva classi CSS.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `classes` | `String` | yes |  | Le classi. |
| `state` | `Boolean` | no |  | Forza lo stato. |

Returns: `Macro` La macro.

Example: Uso di tclass()
```js
macro.tclass('active', true);
console.log(macro.hclass('active'));  // true
```

### self.hclass(name)

Type: `method`

Indica se l'elemento ha una classe CSS.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | La classe. |

Returns: `Boolean` Il risultato.

Example: Uso di hclass()
```js
console.log(macro.hclass('missing'));  // false
```

### self.attr(name, [value])

Type: `method`

Legge o scrive un attributo.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | L'attributo. |
| `value` | `String` | no |  | Il nuovo valore. |

Returns: `String|Macro` Il valore, oppure la macro quando passi `value`.

Example: Uso di attr()
```js
macro.attr('title', 'Orders');
console.log(macro.attr('title'));  // Orders
```

### self.attrd(name, [value])

Type: `method`

Legge o scrive un attributo `data-`.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | Il nome senza il prefisso `data-`. |
| `value` | `String` | no |  | Il nuovo valore. |

Returns: `String|Macro` Il valore, oppure la macro quando passi `value`.

Example: Uso di attrd()
```js
macro.attrd('id', '5');
console.log(macro.attr('data-id'));  // 5
```

### self.attrd2(name)

Type: `method`

Legge un attributo `data-` dall'elemento o dal genitore più vicino che lo possiede.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | Il nome senza il prefisso `data-`. |

Returns: `String` Il valore.

Example: Uso di attrd2()
```js
console.log(macro.attrd2('area'));  // main
```

### self.css(name, [value])

Type: `method`

Legge o scrive una proprietà CSS.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `name` | `String` | yes |  | La proprietà. |
| `value` | `String` | no |  | Il nuovo valore. |

Returns: `String|Macro` Il valore, oppure la macro quando passi `value`.

Example: Uso di css()
```js
macro.css('display', 'block');
console.log(macro.css('display'));  // block
```

### self.visible([callback])

Type: `method`

Indica se l'elemento è visibile.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `callback` | `Function` | no |  | Viene eseguito quando l'elemento è visibile. |

Returns: `Boolean` La visibilità corrente.

Con un callback, la funzione viene eseguita non appena l'elemento è visibile.

Example: Uso di visible()
```js
console.log(macro.visible());  // true
```

### self.width([callback])

Type: `method`

Restituisce la larghezza dell'elemento in pixel.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `callback` | `Function` | no |  | Riceve la larghezza. |

Returns: `Number` La larghezza.

Con un callback, la funzione viene eseguita non appena la larghezza è maggiore di zero.

Example: Uso di width()
```js
macro.css('display', 'block');
macro.css('width', '200px');
console.log(macro.width());  // 200
```

### self.height([callback])

Type: `method`

Restituisce l'altezza dell'elemento in pixel.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `callback` | `Function` | no |  | Riceve l'altezza. |

Returns: `Number` L'altezza.

Con un callback, la funzione viene eseguita non appena l'altezza è maggiore di zero.

Example: Uso di height()
```js
macro.css('display', 'block');
macro.css('height', '50px');
console.log(macro.height());  // 50
```

### self.autofocus([selector])

Type: `method`

Sposta il focus sul primo campo visibile.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `selector` | `String` | no | `input,select,textarea` | I campi. |

Returns: `Macro` La macro.

Sui dispositivi mobili il metodo non fa nulla.

Example: Uso di autofocus()
```js
macro.autofocus();
console.log(document.activeElement.tagName);  // INPUT
```

### self.faicon(value)

Type: `method`

Completa il nome di un'icona.

| Parameter | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `value` | `String` | yes |  | Il nome dell'icona. |

Returns: `String` Le classi CSS.

Un nome senza spazi riceve `DEF.iconprefix`, cioè `ti ti-`.

Example: Uso di faicon()
```js
console.log(macro.faicon('home'));  // ti ti-home
```
