Spanish Total.js
English
Documentation For AI agents
Contents

Values and data binding

How a component holds a value, shares it with a Form or another component, and validates it.

.md GitHub
On this page

Each instance holds its data in instance.state.value. A component that the user edits, such as a field, sets this value; other components read it. This page uses Stepper, a number with a minus and a plus button.

A Stepper inside a Form; To JSON follows the Form and shows quantity 3

The code of the published design

render.html of Stepper:

<style>
    .CLASS label { font-size: 12px; margin-bottom: 5px; display: block; }
    .CLASS_box { display: inline-flex; align-items: center; height: 38px; border: 1px solid #D0D0D0; border-radius: var(--radius); background-color: #FFF; }
    .CLASS_box span { min-width: 50px; text-align: center; font-size: 14px; }
    .CLASS_box button { width: 36px; height: 36px; border: 0; background: none; cursor: pointer; font-size: 16px; }
    .CLASS.UI_touched.UI_invalid .CLASS_box { border-color: red; }
</style>

<script>

    exports.id = 'stepper';
    exports.inputs = [{ id: 'set', name: 'Set value' }];
    exports.outputs = [{ id: 'change', name: 'Change' }];

    exports.make = function(instance, config, element, cls) {

        // A Form reads and writes the value of this component
        instance.state.bind = true;

        var html = '<label>{0}:</label><div class="{1}_box"><button name="minus">−</button><span></span><button name="plus">+</button></div>';
        element.html(html.format(Thelpers.encode(config.name), cls));

        var span = element.find('span');

        element.on('click', 'button', function() {
            var value = (instance.get() || 0) + (this.name === 'plus' ? config.step : -config.step);
            instance.set('value', value);
            instance.set('modified', true);
            instance.set('touched', true);
            instance.output('change', value);
        });

        instance.on('value', function(value) {
            span.text(value == null ? '' : value);
        });

        instance.validate = function() {
            var value = instance.get();
            return value != null && value >= config.min && value <= config.max;
        };

        instance.default = function() {
            return config.min;
        };

        instance.on('reset', function() {
            instance.set('touched', false);
            instance.set('modified', false);
        });

        instance.input('set', function(data) {
            instance.set('value', +data);
        });

        instance.on('ready', function() {
            if (instance.get() == null)
                instance.set('value', config.min);
        });
    };

</script>

Set and read the value

  • instance.set('value', value) sets the value. UI Builder emits value to the instance and passes the value on to the components that follow it.
  • instance.get() returns the value.
  • instance.on('value', fn) runs when the value changes, also when a Form or another component sets it.

set does nothing when the new value equals the current one, unless the value is an object.

The state

instance.set() also sets the other parts of instance.state:

KeyMeaningClass on the element
valueThe data of the instance.
modifiedThe user changed the value.UI_modified
touchedThe user worked with the component.UI_touched
invalidThe value is not valid.UI_invalid
disabledThe component is disabled.UI_disabled
readonlyThe component is read only.UI_readonly

Use the classes in your CSS. .CLASS.UI_touched.UI_invalid shows an error only after the user has worked with the component:

The Stepper with the value 7 above its maximum of 5, with a red border and a disabled SEND button

Validation

instance.validate is a function that you define. It returns true when the value is valid. UI Builder calls it through instance.check(), which runs after every new value and every change of touched, and sets invalid. It returns false, or an error text, when the value is not valid. A Form calls it for every component inside it and disables its button while one is invalid.

instance.default returns the value that a Form sets when it clears its components. Handle reset to clear touched and modified after the form is sent.

Work with a Form

Set instance.state.bind = true in render.html. A Form then:

  • collects the values of the components inside it under their Read value from paths, for example { "quantity": 3 };
  • fills them from the data of its input Load data;
  • sends the collected data from its output Submit.

The path can have dots, for example address.city. See Component settings.

Follow another component

exports.follow = true in editor.html lists every instance of your component in Read value from of other components, as @ and its id. A component whose path is @id follows that instance: it receives every new value of it, and a value it sets goes back to that instance.

In the picture above, To JSON has the path @ plus the id of the Form, so it shows the data of the form as the user clicks.

instance.datasource('@id', fn) reads the value of another instance as a list and calls fn again every time it changes. Components with a Data source field use it.

Notify instead of bind

With instance.state.notify = true, a following instance does not get the new value: UI Builder emits notify with the value instead, and your code decides what to do. Counter uses it to count the changes.

Made in Europe