EnginioModel QML Type | Enginio QML Types and Examples 1.2

- Enginio Manual
- Enginio QML Types and Examples
- Enginio 1.2.1 Reference Documentation

# EnginioModel QML Type

Makes accessing collections of objects easy More...

Import Statement: | import Enginio 1.0
Since: | Qt 5.3

- List of all members, including inherited members

## Properties

- client : EnginioClient
- operation : Enginio::Operation
- query : QJSValue

## Methods

- EnginioReply append(QJSValue object)
- EnginioReply remove(int row)
- EnginioReply setProperty(int row, string propertyName, QVariant value)

## Detailed Description

    import Enginio 1.0

The query property of the model takes a JSON object.

To get a model of each object of type "objects.city" use:

    EnginioModel {
        id: enginioModel
        client: client
        query: { "objectType": "objects.city" }
    }

It is then possible to use a regular Qt Quick ListView to display the list of cities that the backend contains.

    ListView {
        anchors.fill: parent
        model: enginioModel
        delegate: Text {
            text: name + ": " + population
        }
    }

Note that the properties of the objects can be directly accessed. In this example, we have the type "objects.city" in the backend with two properties: "name" and "population".

The model supports several function to modify the data, for example append(), remove(), setProperty()

The QML version of EnginioModel supports the same functionality as the C++ version. EnginioModel C++

## Property Documentation

client : EnginioClient

The instance of EnginioClient used for this model.

operation : Enginio::Operation

The operation used for the query.

query : QJSValue

The query used to populate the model with data from the backend.

See EnginioClient::query() documentation for information on how to construct a query.

While the EnginioClient implementation of `query()` returns the data of a query as a JSON object, for the model the query will be interpreted as the model data.

Usually, the query is for a single object type and will return all objects in the database of that type. The model will then represent each returned object as one row. It can be limited and sorted just like its counterpart in EnginioClient.

One important thing to note is that the model cannot keep the same sorting as the backend, and thus sorting and limits are only preserved until an insertion or deletion happens.

See also EnginioClient::query().

## Method Documentation

EnginioReply append(QJSValue object)

Add a new object to the model and database.

This function appends the new object to the local model cache and makes an asynchronous request to the backend.

Since adding an object to the database may fail for various reasons, the returned reply must be kept and used for error handling (see EnginioReply). If the operation fails, the object that was supposed to be appended will be removed from the local model again. If the model is used in a view and the backend does not accept the object because it violates a validator, it will be visible to the user that a new row in the view appears and disappears again.

Returns the EnginioReply from the backend

To add a "city" object to the model by appending it:

    function addCity() {
        var berlin = {
            "objectType": "objects.city",
            "name": "Berlin",
            "population": 3300000
        }
        enginioModel.append(berlin)
    }

EnginioReply remove(int row)

Removes the row from the model and database.

This function immediately removes the row from the local cache and sends a remove request to the Enginio backend.

Returns the EnginioReply from the backend.

See also EnginioClient::remove().

EnginioReply setProperty(int row, string propertyName, QVariant value)

Change a property of an object in the model

The property propertyName of the object at row will be set to value. The model will locally reflect the change immediately and propagage the change to the server in the background.