Spanish Total.js
English
Documentation For AI agents
Contents

Getting started

Build a working page with bound data and a custom component, starting from an empty file.

.md GitHub
On this page

This guide takes you from an empty file to a working page with a custom input component and data bound to it. You need a text editor and a browser.

Create the page

Create a file named index.html and load the library from the CDN:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>My first page</title>
    <link rel="stylesheet" href="https://cdn.componentator.com/spa.min@20.css" />
    <script src="https://cdn.componentator.com/spa.min@20.js"></script>
</head>
<body>

</body>
</html>

All the following code goes inside <body>. Open the file directly in the browser to see the result.

Show a value

Write a value to the model with SET() and display it with a <ui-bind> element:

<h1>Hello <ui-bind path="app.name" config="text"></ui-bind></h1>

<script>
    SET('app.name', 'Ana');
</script>

The page shows Hello Ana.

  • SET('app.name', 'Ana') creates the global object app and stores the value in app.name.
  • path tells <ui-bind> which value to listen to.
  • config="text" tells it to render the value as text.

Every later SET('app.name', ...) updates the heading. Read more in Paths and Data binding.

Create a component

A component is a reusable widget. Declare it once with COMPONENT() and place it on the page with <ui-component>. Replace the content of <body> with this code:

<h1>Hello <ui-bind path="app.name" config="text;empty:stranger"></ui-bind></h1>

<ui-component name="textbox" path="app.name" config="label:Your name;required:true"></ui-component>

<script>
COMPONENT('textbox', 'label:Name', function(self, config, cls) {

    self.make = function() {
        self.aclass(cls);
        self.html('<label></label><input type="text" />');
        self.autobind20();
    };

    self.configure = function(key, value) {
        if (key === 'label')
            self.find('label').text(value);
    };

    self.validate = function(value) {
        return !config.required || !!value;
    };

}, null, '.CLASS label { display: block; font-weight: bold; } .CLASS.ui-invalid input { border-color: red; }');

SET('app.name', 'Ana');
</script>

Type into the field and the heading follows your text. Clear the field and the heading shows Hello stranger, because empty:stranger in the <ui-bind> configuration sets the text for an empty value.

Here is what each part of the declaration does:

CodePurpose
'textbox'The name used in <ui-component name="textbox">
'label:Name'The default configuration, which the config attribute can override
self.makeRuns once for each instance and creates its HTML
self.aclass(cls)Adds the CSS class ui-textbox to the element
self.autobind20()Writes the content of the input to the model while the user types
self.configureRuns for each configuration key
self.validateSays if the current value is valid. The library then adds or removes the class ui-invalid
The last argumentCSS of the component, where CLASS stands for ui-textbox

The component does not define how to show the value in the input. When a component has no setter of its own, the library writes the value into the first input, select or textarea inside the component.

Run code from a button

Elements with the class exec call the function named in their data-exec attribute when they are clicked. Add a button that clears the model:

<h1>Hello <ui-bind path="app.name" config="text;empty:stranger"></ui-bind></h1>

<ui-component name="textbox" path="app.name" config="label:Your name;required:true"></ui-component>

<button class="exec" data-exec="FUNC.clear">Clear</button>

Then add the function to the script, above the SET() call:

FUNC.clear = function() {
    SET('app.name', '');
};

FUNC is an empty global object meant for your own functions. The click changes only the model. The input, the heading and the validation state all follow.

The complete page

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>My first page</title>
    <link rel="stylesheet" href="https://cdn.componentator.com/spa.min@20.css" />
    <script src="https://cdn.componentator.com/spa.min@20.js"></script>
</head>
<body>

    <h1>Hello <ui-bind path="app.name" config="text;empty:stranger"></ui-bind></h1>

    <ui-component name="textbox" path="app.name" config="label:Your name;required:true"></ui-component>

    <button class="exec" data-exec="FUNC.clear">Clear</button>

    <script>
        COMPONENT('textbox', 'label:Name', function(self, config, cls) {

            self.make = function() {
                self.aclass(cls);
                self.html('<label></label><input type="text" />');
                self.autobind20();
            };

            self.configure = function(key, value) {
                if (key === 'label')
                    self.find('label').text(value);
            };

            self.validate = function(value) {
                return !config.required || !!value;
            };

        }, null, '.CLASS label { display: block; font-weight: bold; } .CLASS.ui-invalid input { border-color: red; }');

        FUNC.clear = function() {
            SET('app.name', '');
        };

        SET('app.name', 'Ana');
    </script>

</body>
</html>

If something does not work

  • The component stays empty. Check that the name attribute matches the name in COMPONENT(). For an unknown name, the library tries to download the component from the CDN and writes jComponent: Downloading "name" component. to the console.
  • The value does not change on the page. Change the model with SET(). A direct assignment such as app.name = 'Ana' does not notify anybody until you call UPD('app.name').

Next steps

Made in Europe