ModelDefinition

model. ModelDefinition

Definition of a Model. Basically this object contains the meta data related to the Model. Like name, apiEndPoint, modelValidation, etc. It also has methods to create and load Models that are based on this definition. The Data element ModelDefinition would be used to create Data Element Models

Note: ModelDefinition has a property api that is used for the communication with the dhis2 api. The value of this property is an instance of Api.

Constructor

new ModelDefinition()

Source:

Methods

(static) createFromSchema(schema, attributes) → {ModelDefinition}

Source:
Example
ModelDefinition.createFromSchema(schemaDefinition, attributes);
Parameters:
Name Type Description
schema Object

A schema definition received from the web api (/api/schemas)

attributes Array.<Object>

A list of attribute objects that describe custom attributes (/api/attributes)

Returns:

Frozen model definition object.

Type
ModelDefinition

create(dataopt) → {Model}

Source:

Creates a fresh Model instance based on the ModelDefinition. If data is passed into the method that data will be loaded into the matching properties of the model.

Example
dataElement.create({name: 'ANC', id: 'd2sf33s3ssf'});
Parameters:
Name Type Attributes Description
data Object <optional>

Data values that should be loaded into the model.

Returns:

Returns the newly created model instance.

Type
Model

delete() → {Promise}

Source:

This method is used by the Model instances to delete the model when calling model.delete().

Returns:

Returns a promise to the deletion operation

Type
Promise

get(identifier, queryParamsopt) → {Promise}

Source:

Get a Model instance from the api loaded with data that relates to identifier. This will do an API call and return a Promise that resolves with a Model or rejects with the api error message.

Example
//Do a get request for the dataElement with given id (d2sf33s3ssf) and print it's name
//when that request is complete and the model is loaded.
dataElement.get('d2sf33s3ssf')
  .then(model => console.log(model.name));
Parameters:
Name Type Attributes Default Description
identifier String
queryParams Object <optional>
{fields: ':all'}

Query parameters that should be passed to the GET query.

Returns:

Resolves with a Model instance or an error message.

Type
Promise

getOwnedPropertyNames() → {Array.<String>}

Source:

This method returns a list of property names that that are defined as "owner" properties on this schema. This means these properties are used when saving the model to the server.

Example
dataElement.getOwnedPropertyNames()
Returns:

Returns an array of property names.

Type
Array.<String>

getTranslatableProperties() → {Array.<String>}

Source:

These properties can be translated using the DHIS2 database translations.

Returns:

Returns a list of property names on the object that are translatable.

Type
Array.<String>

getTranslatablePropertiesWithKeys() → {Array.<Object>}

Source:

This method is similar to getTranslatableProperties() but in addition to the property names also returns the translationKey that is used to save the translations for the property names.

Returns:

Returns an array with objects that have name and translationKey properties.

Type
Array.<Object>

isTranslatable() → {Boolean}

Source:

Check for if the Model supports translations

Returns:

True when the schema can be translated, false otherwise

Type
Boolean

list(listParamsopt) → {Promise}

Source:

Loads a list of models.

Example
// Loads a list of models and prints their name.
dataElement.list()
  .then(modelCollection => {
    modelCollection.forEach(model => console.log(model.name));
  });
Parameters:
Name Type Attributes Default Description
listParams Object <optional>
{fields: ':all'}

Query parameters that should be passed to the GET query.

Returns:

ModelCollection collection of models objects of the ModelDefinition type.

Type
Promise

save(model) → {Promise}

Source:

This method is used by the Model instances to save the model when calling model.save().

Parameters:
Name Type Description
model Model

The model that should be saved to the server.

Returns:

A promise which resolves when the save was successful or rejects when it failed. The promise will resolve with the data that is returned from the server.

Type
Promise