Flags and path modifiers
How flags change the behaviour of an operation on a path and how path modifiers shorten paths.
On this page
A flag is a word that starts with @ and follows a path, separated by a space: SET('form.name @change', 'Ana'). Flags tell the library, the components and your own code how to treat the operation. A path modifier is a character that changes what the path points to, such as * in *title.
Passing flags
Add any number of flags after the path. Watchers and component setters receive them as an object. A flag can carry a value in the form @name:value.
WATCH('form.name', function(value, flags) {
console.log(value, flags);
});
SET('form.name @mine', 'Ana');
SET('form.name @mode:edit @type:3', 'Peter');Ana {"mine":1}
Peter {"mode":"edit","type":3}The value of the flag @type is converted to a number when it starts with a digit. Values of other flags stay strings.
Flags for the state of components
Each component tracks whether the user has touched and modified it. These flags change that state for all components on the path:
| Flag | Effect |
|---|---|
@change, @touch, @modify, @modified | Marks the components as touched and modified |
@reset | Clears the touched and modified state |
@default | Clears the state and fills empty values from the default attribute of the component |
<ui-component id="name" name="field" path="form.name"></ui-component>ON('ready', function() {
var el = document.getElementById('name');
SET('form.name @change', 'Ana');
console.log(el.className);
SET('form.name @reset', 'Peter');
console.log('[' + el.className + ']');
SET('form.name @touch', 'Eva');
console.log(el.className);
GET('form.name @reset');
console.log('[' + el.className + ']');
});ui-touched ui-modified
[]
ui-touched ui-modified
[]The state is visible in the CSS classes ui-touched and ui-modified. GET() with a flag notifies the path and returns the value, so GET('form.name @reset') resets the state without changing the data. RESET(path) does the same for a whole path.
The flag @default uses the value of the default attribute when the model contains null or undefined:
<ui-component name="field" path="form.age" default="18"></ui-component>ON('ready', function() {
SET('form.age', 30);
console.log(form.age);
SET('form.age @default', null);
console.log(form.age);
});30
18Filtering components
COMPONENTS(path) returns the components on a path. Flags limit the result:
| Flag | Returns the components that are |
|---|---|
@touched | touched |
@modified | modified |
@required | configured with required:true |
@invalid | invalid |
@valid | valid |
@disabled | disabled |
@enabled | not disabled |
@visible | visible on the page |
@hidden | hidden on the page |
<ui-component name="field" path="form.name" config="required:true"></ui-component>
<ui-component name="field" path="form.email" config="required:true"></ui-component>
<ui-component name="field" path="form.note" config="disabled:true"></ui-component>COMPONENT('field', function(self, config) {
self.validate = function(value) {
return !config.required || !!value;
};
});
ON('ready', function() {
SET('form.name @change', 'Ana');
const names = list => list.map(com => com.path.toString());
console.log(names(COMPONENTS('form')));
console.log(names(COMPONENTS('form @required')));
console.log(names(COMPONENTS('form @invalid')));
console.log(names(COMPONENTS('form @modified')));
console.log(names(COMPONENTS('form @disabled')));
});["form.name","form.email","form.note"]
["form.name","form.email"]
["form.email"]
["form.name"]
["form.note"]Forms and validation shows how to use the state in a form.
Waiting for a target
EXEC() and SETTER() do nothing when the function or the component does not exist yet. With the flag @important, they try again every 500 milliseconds until the target appears.
EXEC('FUNC.start @important', 'now');
setTimeout(function() {
FUNC.start = function(value) {
console.log('started', value);
};
}, 100);started nowCustom flags
A custom flag emits the event @flag name, and the handler receives the path. UPD(), EMIT() and FLAGS(flags, [callback]) accept custom flags:
ON('@flag saved', function(path) {
console.log('saved:', path);
});
SET('invoice.total', 100);
UPD('invoice.total @saved');
FLAGS('@saved @other', function() {
console.log('flags processed');
});
EMIT('finished @saved');saved: invoice.total
saved:
flags processed
saved:Path modifiers
| Modifier | Example | Points to |
|---|---|---|
* | *title | common.title |
% | %search | DEF.tmp.search, a place for temporary values |
# | #languages | DEF.cl.languages, a code list |
[key] | [section].count | The part in brackets is replaced by the environment value set with ENV() |
#name | user.language #languages | With SET(), the value is written after the code list languages has loaded |
<ui-bind id="a" path="*title" config="text"></ui-bind>
<ui-bind id="b" path="#languages" config="text:value.length"></ui-bind>SET('*title', 'Common title');
console.log(common.title);
SET('%search', 'pen');
console.log(DEF.tmp.search);
ENV('section', 'invoices');
SET('[section].count', 5);
console.log(invoices.count);
CLINIT('languages', function(next) {
next([{ id: 'en' }, { id: 'sk' }]);
});
SET('user.language #languages', 'sk');
ON('ready', function() {
CL('languages', function() {
console.log(user.language, GET('#languages').length);
});
});Common title
pen
5
sk 2CLINIT(name, callback) declares how a code list loads, and CL(name, callback) runs a function when it is available.
Paths in plugins
| Modifier | Example | Points to |
|---|---|---|
? | ?.name | The model of the closest parent plugin |
?| | ?|title | A property of the closest parent plugin instance |
| | contact|title | A property of the plugin registered as contact |
<ui-plugin path="contact">
<ui-bind path="?.name" config="text"></ui-bind>
</ui-plugin>
<ui-bind id="outside" path="contact|title" config="text"></ui-bind>PLUGIN('contact', function(exports) {
exports.title = 'Contact form';
exports.make = function() {
exports.set('name', 'Ana');
};
});The first <ui-bind> shows Ana and the second shows Contact form. Read more in Plugins.
Prefixes of functions and elements
| Prefix | Used in | Effect |
|---|---|---|
^ | PUSH('^list', value) | Inserts the value at the start of the array |
! | <ui-bind path="!user.name"> | Skips rendering while the value is null or undefined |
! | SETTER('!dialog/open') | Does not create a lazy component |