Macro
The delegates, properties and methods of a macro instance.
On this page
API
delegate
self.make = function()
Runs once after the declaration.
self.make = function()<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.
self.setter = function(value, path, type)| Parameter | Type | Description |
|---|---|---|
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.
<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.
self.service = function(counter)| Parameter | Type | Description |
|---|---|---|
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.
self.destroy = function()<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.
self.nameconsole.log(macro.name); // demo
property
self.path
The path of the binder.
self.pathconsole.log(macro.path); // form.name
property
self.element
The element of the binder as a jQuery object.
self.elementconsole.log(macro.element.attr('id')); // box
property
self.dom
The element of the binder as a DOM node.
self.domconsole.log(macro.dom.id); // box
property
self.binder
The binder that created the macro.
self.binder
property
self.scope
The Scope of the binder.
self.scopeThe property is assigned after the make delegate and exists when the path contains ?.
console.log(macro.scope.path); // users
method
self.get()
Reads the value of the path.
self.get()Returns Object The value.
SET('form.name', 'Peter');
console.log(macro.get()); // Peter
method
self.set(value)
Writes a value to the path.
self.set(value)| Parameter | Type | Description |
|---|---|---|
value |
Object |
The new value. |
macro.set('Peter');
console.log(GET('form.name')); // Peter
method
self.refresh()
Runs the binder again with the current value.
self.refresh()Returns Macro The macro.
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.
self.EXEC(path, [a], [b], [c], [d])| Parameter | Type | Description |
|---|---|---|
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.
macro.EXEC('@ping', 5);
method
self.SEEX(path, [a], [b], [c], [d])
Calls a function or writes a value.
self.SEEX(path, [a], [b], [c], [d])| Parameter | Type | Description |
|---|---|---|
path |
String |
The path. |
Returns Macro The macro.
When the path holds no function, the first argument is written to it.
macro.SEEX('@title', 'Orders');
console.log(FIND('owner').title); // Orders
method
self.find(selector)
Finds nested elements.
self.find(selector)| Parameter | Type | Description |
|---|---|---|
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.
console.log(macro.find('.label').text()); // Text
method
self.closest(selector)
Finds the closest matching parent.
self.closest(selector)| Parameter | Type | Description |
|---|---|---|
selector |
String |
The selector. |
Returns jQuery The result.
console.log(macro.closest('body').length); // 1
method
self.parent([selector])
Returns a parent element.
self.parent([selector])| Parameter | Type | Description |
|---|---|---|
selector? |
String |
The selector. |
Returns jQuery The result.
The argument accepts the same values as parent() of a component.
console.log(macro.parent().prop('tagName')); // BODY
method
self.html([value])
Reads or replaces the HTML content.
self.html([value])| Parameter | Type | Description |
|---|---|---|
value? |
String|Array |
The new content. |
Returns String|jQuery The content, or the element when value is passed.
An array is joined.
macro.html('<i>A</i>');
console.log(macro.html()); // <i>A</i>
method
self.text([value])
Reads or replaces the text content.
self.text([value])| Parameter | Type | Description |
|---|---|---|
value? |
String|Array |
The new content. |
Returns String|jQuery The content, or the element when value is passed.
An array is joined.
macro.text('A');
console.log(macro.text()); // A
method
self.append(value)
Adds content to the end of the element.
self.append(value)| Parameter | Type | Description |
|---|---|---|
value |
String|Array |
The content. |
Returns jQuery The element.
An array is joined.
macro.append('<i>A</i>');
console.log(macro.find('i').length); // 1
method
self.empty()
Removes the content of the element.
self.empty()Returns jQuery The element.
macro.empty();
console.log(macro.html() === ''); // true
method
self.event(name, [selector], callback)
Attaches a jQuery event to the element.
self.event(name, [selector], callback)| Parameter | Type | Description |
|---|---|---|
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.
macro.event('click', '.label', () => console.log('clicked')); // clicked
macro.find('.label').click();
method
self.aclass(classes, [timeout])
Adds CSS classes.
self.aclass(classes, [timeout])| Parameter | Type | Description |
|---|---|---|
classes |
String |
The classes. |
timeout? |
Number |
The delay in milliseconds. |
Returns Macro The macro.
macro.aclass('active');
console.log(macro.hclass('active')); // true
method
self.rclass([classes], [timeout])
Removes CSS classes.
self.rclass([classes], [timeout])| Parameter | Type | Description |
|---|---|---|
classes? |
String |
The classes. |
timeout? |
Number |
The delay in milliseconds. |
Returns Macro The macro.
Without an argument, all classes are removed.
macro.aclass('active');
macro.rclass('active');
console.log(macro.hclass('active')); // false
method
self.rclass2(search)
Removes CSS classes that contain a text.
self.rclass2(search)| Parameter | Type | Description |
|---|---|---|
search |
String|RegExp |
The text or a regular expression. |
Returns Macro The macro.
macro.aclass('col-1 col-2');
macro.rclass2('col-');
console.log(macro.hclass('col-2')); // false
method
self.tclass(classes, [state])
Toggles CSS classes.
self.tclass(classes, [state])| Parameter | Type | Description |
|---|---|---|
classes |
String |
The classes. |
state? |
Boolean |
Forces the state. |
Returns Macro The macro.
macro.tclass('active', true);
console.log(macro.hclass('active')); // true
method
self.hclass(name)
Tells whether the element has a CSS class.
self.hclass(name)| Parameter | Type | Description |
|---|---|---|
name |
String |
The class. |
Returns Boolean The result.
console.log(macro.hclass('missing')); // false
method
self.attr(name, [value])
Reads or writes an attribute.
self.attr(name, [value])| Parameter | Type | Description |
|---|---|---|
name |
String |
The attribute. |
value? |
String |
The new value. |
Returns String|Macro The value, or the macro when value is passed.
macro.attr('title', 'Orders');
console.log(macro.attr('title')); // Orders
method
self.attrd(name, [value])
Reads or writes a data- attribute.
self.attrd(name, [value])| Parameter | Type | Description |
|---|---|---|
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.
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.
self.attrd2(name)| Parameter | Type | Description |
|---|---|---|
name |
String |
The name without the data- prefix. |
Returns String The value.
console.log(macro.attrd2('area')); // main
method
self.css(name, [value])
Reads or writes a CSS property.
self.css(name, [value])| Parameter | Type | Description |
|---|---|---|
name |
String |
The property. |
value? |
String |
The new value. |
Returns String|Macro The value, or the macro when value is passed.
macro.css('display', 'block');
console.log(macro.css('display')); // block
method
self.visible([callback])
Tells whether the element is visible.
self.visible([callback])| Parameter | Type | Description |
|---|---|---|
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.
console.log(macro.visible()); // true
method
self.width([callback])
Returns the width of the element in pixels.
self.width([callback])| Parameter | Type | Description |
|---|---|---|
callback? |
Function |
Receives the width. |
Returns Number The width.
With a callback, the function runs as soon as the width is greater than zero.
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.
self.height([callback])| Parameter | Type | Description |
|---|---|---|
callback? |
Function |
Receives the height. |
Returns Number The height.
With a callback, the function runs as soon as the height is greater than zero.
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.
self.autofocus([selector])| Parameter | Type | Description |
|---|---|---|
selector? |
String |
The fields. Default: input,select,textarea |
Returns Macro The macro.
The method does nothing on mobile devices.
macro.autofocus();
console.log(document.activeElement.tagName); // INPUT
method
self.faicon(value)
Completes the name of an icon.
self.faicon(value)| Parameter | Type | Description |
|---|---|---|
value |
String |
The name of the icon. |
Returns String The CSS classes.
A name without a space receives DEF.iconprefix, which is ti ti-.
console.log(macro.faicon('home')); // ti ti-home