Spanish Total.js
English
Documentation For AI agents
Contents

FAQ

Answers to the questions developers ask most often when they start with jComponent.

.md GitHub
On this page

Short answers to the questions that come up first. Each answer links to the page that explains the topic in full.

Do I need a build step?

  1. jComponent is a script that you include in the page. The published build also contains jQuery, the Tangular template engine and the routing. See Getting started.

Why does the page not change when I assign a value to a variable?

A plain assignment does not tell the library that anything happened. Use SET(), or call UPD() after you change the object yourself:

<script>
    var user = { name: 'Peter' };
</script>

<ui-bind path="user.name" config="text"></ui-bind>

<script>
    user.name = 'Lucia';       // The page still shows Peter
    UPD('user.name');          // The page shows Lucia
    SET('user.name', 'Anna');  // The page shows Anna
</script>

See Paths and Data binding.

How do I run code when a value changes?

Use WATCH(). The callback receives the path, the value and the type of the change:

WATCH('cart.total', function(path, value, type) {
    console.log(path, value);
});

SET('cart.total', 5); // cart.total 5

See Data binding.

Why does FIND() return nothing when my script starts?

Components are created after the scripts of the page have run. On a page with components, wait for the ready event:

ON('ready', function() {
    var component = FIND('hello');
});

See Using components and the events reference.

When should I use a component, a plugin or a macro?

  • A component is a reusable piece of interface, such as an input or a grid.
  • A plugin holds the data and logic of a part of the application, such as a page or a form.
  • A macro adds a small behaviour to a single <ui-bind> element.

What does the question mark in a path mean?

Inside <ui-plugin>, ? stands for the path of the plugin. With the plugin orders, ?.items is orders.items and ?/refresh is the method refresh of the plugin. See Scopes.

How do I call a plugin method from HTML?

Add the class exec and name the method in data-exec:

<ui-plugin path="counter">
    <button class="exec" data-exec="?/increment">+1</button>
</ui-plugin>

From JavaScript, use EXEC('counter/increment'). See Plugins.

How do I render a list from an array?

Use the template command of <ui-bind> with a Tangular loop. The list is rendered again when the array changes:

<script>
    var products = [{ name: 'Keyboard' }, { name: 'Mouse' }];
</script>

<ui-bind path="products" config="template">
    <script type="text/html">
        {{ foreach item in value }}<li>{{ item.name }}</li>{{ end }}
    </script>
</ui-bind>

PUSH('products', { name: 'Monitor' }) adds a third item to the page. See Templates with Tangular.

Are values in templates safe against HTML injection?

{{ value }} is encoded. A command with a helper is not encoded unless you add the encode helper at the end, as in {{ value | upper | encode }}, and raw writes the value as it is. See Templates with Tangular.

How do I load part of a page on demand?

Use IMPORT('/parts/form.html', '#target') from code or <ui-import config="url:/parts/form.html"> in the markup. See Importing content.

Why did the script of an imported file run only once?

<ui-import> removes scripts and styles when the same address is imported again. Add reevaluate:true to the config attribute if each copy has to run its script. See Importing content.

How do I show a "page not found" screen?

Listen to the 404 event, which is emitted when no route matches the address:

ON('404', function() {
    SET('common.page', 'notfound');
});

See Routing.

Why is my route never called?

Check the case and the number of segments. Addresses are compared in lowercase, so ROUTE('/Admin/', fn) never matches. Write it as ROUTE('/admin/', fn). See Routing.

How do I protect a route?

Declare a middleware with MIDDLEWARE(), name it in the route, and call next(false) to stop the navigation. See Routing.

Why is my required field marked invalid on a fresh form?

An empty required field is invalid from the start, so it has the ui-invalid class right away. Combine the classes in your CSS to show the error only after the user has touched the field:

.ui-touched.ui-invalid input { border-color: red; }

See Forms and validation.

Why is my save button still disabled when the form is valid?

CAN() needs a valid form and at least one field the user has changed. Use VALID() if you only care about validity. See Forms and validation.

Why is the text in my HTML not translated?

@(text) is translated in component configuration, in <ui-bind> templates and in imported content. Static HTML of the page stays as it is. See Localization.

How do I change the date and number formats?

Set DEF.dateformat, DEF.thousandsseparator and DEF.decimalseparator when the application starts. See Localization.

Made in Europe