Spanish Total.js
English
Documentation For AI agents
Contents

Flags and path modifiers

Change how a read or write behaves by adding flags and modifiers to a path.

.md GitHub
On this page

A path can carry extra instructions. Flags start with @ and follow the path after a space, as in SET('price @nowatch', 5). Path modifiers are characters that change what the operation does, as in SET('+tags', 'red'). Several flags can be combined in one path.

Flags for the state of components

Components keep track of whether the user touched or modified them. These flags set that state while writing the value:

FlagEffect on the components at the path
@touchMarks them as touched.
@changeMarks them as touched and modified.
@modifyMarks them as touched and modified.
@resetClears touched and modified.
@defaultWrites the default value of each component and clears touched and modified.
<ui-component id="age" name="field" path="person.age" default="18"></ui-component>

<script>
    COMPONENT('field', function(self, config) {
        self.setter = function(value) {
            self.text(value);
        };
    });

    ON('ready', function() {

        var com = FIND('field');
        var show = label => console.log(label, com.get(), 'touched=' + com.config.touched, 'modified=' + com.config.modified);

        SET('person.age @touch', 20);
        show('@touch');     // @touch 20 touched=true modified=false

        SET('person.age @change', 30);
        show('@change');    // @change 30 touched=true modified=true

        SET('person.age @reset', 40);
        show('@reset');     // @reset 40 touched=false modified=false

        SET('person.age @default', 50);
        show('@default');   // @default 18 touched=false modified=false
    });
</script>

Flags for notifications

FlagEffect
@nowatchDoes not call the watchers of the path.
@type:NReports the number N as the type of the change.
@diffSkips the write when the new value equals the current one.
WATCH('price', (path, value, type) => console.log('watch', value, type));

SET('price', 10);            // watch 10 1
SET('price @nowatch', 20);   // (no output)
SET('price @type:5', 30);    // watch 30 5
SET('price @diff', 30);      // (no output, the value is the same)
SET('price @diff', 40);      // watch 40 1

The flag @nobind works with REWRITE(), which stores a value without updating components. With the flag, bindings are not updated either:

<ui-bind path="note" config="text"></ui-bind>

<script>
    SET('note', 'first');

    setTimeout(function() {
        REWRITE('note @nobind', 'second');
        console.log(note);                                     // second
        setTimeout(() => console.log(document.querySelector('ui-bind').textContent), 100);   // first
    }, 200);
</script>

Flags for reading

FlagWhat GET() does
@cloneReturns a copy of the value.
@modifiedReturns an object with only the fields whose components are touched and modified, or null when there are none.
@resetReturns the value and clears the state of the components at the path.
@updateReturns the value and then notifies the dependents of the path.
<ui-component name="field" path="user.name"></ui-component>
<ui-component name="field" path="user.city"></ui-component>

<script>
    COMPONENT('field', function(self) {
        self.setter = NOOP;
    });

    SET('user', { name: 'Ana', city: 'Rome' });

    ON('ready', function() {

        var copy = GET('user @clone');
        console.log(copy === user);          // false

        SET('user.city @change', 'Oslo');
        console.log(GET('user @modified'));  // {"city":"Oslo"}

        GET('user @reset');
        console.log(GET('user @modified'));  // null
    });
</script>
WATCH('stock', (path, value) => console.log('watch', value));

window.stock = 5;
console.log(GET('stock @update'));   // 5
// watch 5

Custom flags

Any other name is a custom flag. Listen for it with ON('@flag name', fn). The function receives the path, the value and the type. Custom flags also work with EXEC() and EMIT(), where the path argument is the name that was called.

ON('@flag audit', function(path, value, type) {
    console.log('audit:', path, value);
});

function ping() {}

SET('invoice.total @audit', 90);   // audit: invoice.total 90
GET('invoice.total @audit');       // audit: invoice.total 90
EXEC('ping @audit');               // audit: ping undefined
EMIT('saved @audit');              // audit: saved undefined

Add to arrays and merge objects

SyntaxEffect
SET('+path', value)Adds the value to the array, like PUSH().
PUSH('^path', value)Adds the value to the start of the array.
SET('~path', object)Merges the object into the current value, like EXT().
SET('path @extend', object)Merges the object as well.
SET('+tags', 'red');
SET('+tags', 'green');
console.log(tags);            // ["red","green"]

PUSH('^tags', 'first');
console.log(tags);            // ["first","red","green"]

SET('user', { name: 'Ana', age: 30 });
SET('~user', { age: 31 });
console.log(user);            // {"name":"Ana","age":31}

SET('user @extend', { city: 'Rome' });
console.log(user);            // {"name":"Ana","age":31,"city":"Rome"}

Transform a value

An arrow after the path runs an expression on the value. Both --> and -> work. With SET() the result is stored, and with GET() the result is returned while the stored value stays the same. The expression must use the word value. The same syntax works in the path attribute of <ui-bind>.

<script>
    SET('code --> value.toUpperCase()', 'abc');
    console.log(code);                     // ABC

    console.log(GET('code --> value.length'));  // 3
    console.log(GET('code -> value + "!"'));    // ABC!
</script>
<ui-bind path="code --> value.toLowerCase()" config="text"></ui-bind>

Wait for a code list

A #name after the path postpones the write until the code list name is loaded. Code lists are declared with CL_INIT().

CL_INIT('colors', function(next) {
    setTimeout(() => next(['red', 'green']), 100);
}, true);

SET('form.color #colors', 'red');
console.log(window.form);       // undefined

setTimeout(function() {
    console.log(form.color, DEF.cl.colors);   // red ["red","green"]
}, 300);
Made in Europe