Asynchronous Views

Data are usually stored on the server side. If data are loaded asynchronously, you need to wait until they are available to perform data-related operation with views, e.g. select a record. If the UI is built much faster than data are loaded, it is a good idea to make use of promises to load the data. The app will wait for data from the server, and only after a promise resolves, it will render the view.

Promises are returned by all Ajax requests within Webix:

webix.ajax()

A Promise Returned by config() of a Class View

If your client-side depends on server-side data, you can fetch the data asynchronously and return a promise of them in the config() lifetime handler of a Jet view. A promise can be returned by webix.ajax()arrow-up-right. After the promise resolves, the response is passed to a callback in then().

For example, let's load chart configuration with webix.ajax():

//views/statistics.js
import {JetView} from "webix-jet";

export class StatisticsView extends JetView {
    config() {
        return webix.ajax("data/colors").then( data => {
            const colors = data.json();
            const chart = { view:"chart", series:[]};
            for (let i = 0; i < colors.length; i++)
                chart.series.push({
                    value:"#"+i+"#", color:colors[i]
                });
            return chart;
        });
    }
}

webix.ajax() sends a request to server/colors.php and returns a promise of data instead of real data. First, all the data come to the client side and only after that the final view configuration will be constructed and the view will be rendered.

A Simple View as a Promise

You can define a view as a promise of a view object:

Let's load data into a list inside a simple promised view:

More explicitly, the same list view can be defined like this:

data.waitData

waitDataarrow-up-right is used to catch the moment when data are loaded into data components, such as DataCollection, List, Tree, DataTable, etc.

data is a model with Webix DataCollection, e.g.:

webix.promise

Webix offers an interface for working with promise objects. webix.promisearrow-up-right features a set of methods that duplicate Promise object methodsarrow-up-right.

Waiting for Data from Multiple Sources

Promises are great when you need to wait until several data sources are available on the client.

For example, these are data returned by two models, the first dataset is linked to the second via IDs:

Use webix.promise.all() to wait till both data models are loaded and then do something with them:

webix.promise has more useful methods, e.g.:

A Simple View that Returns a Promise

Have a look at a simple example of a factory function that returns a promise that resolves with a template in a second. The delay is done purely for the demo.

Promises in Class Views

webix.promise can also return promises from config() of a class view:

Check out the demo >>arrow-up-right

Last updated