The properties section allows you to add additional properties to the model elements. They can be both scalar (string, number) and computed.
Computed properties are a powerful mechanism that can greatly simplify the implementation of complex business logic in an application. Such a property is a function that is called when an attempt is made to read (or set) a property. The result returned by the function is considered as the value of the property. During the calculations, the function can refer to other computed properties, etc. In this way, it is easy to build dependent data handlers without using the event model.
Computed properties can either supplement existing static properties in the model, or replace existing ones.
The object describing the properties in the template is a regular JavaScript object. The property names of this object are the fully qualified name of the property (including the name of the object type), and the values are a scalar type, function, or object.
The TypeName
property prefix must match the type name, that is specified when the model is built. By
convention, type names begin with an uppercase "T" (for Type).
If a computed property has the same name as a property in the model, then it is not added to the model, but replaces the model property. This will cause the computed value to be saved in the model.
If the property name begins with the characters "$" or "_", then it is internal and does not participate in the exchange of data between the client and the server. If a property name begins with "$$", then changing it does not change the $dirty flag for the model.
The value of a scalar property is always a primitive JavaScript type such as String
, Number
or Boolean
. A property of the corresponding type will be added to the model.
The value of a computed property is the function that will be computed when an attempt is made to access the property. Function argument:
this
- a reference to the object to which the property is added.The function is called only when the property is accessed. If there are no calls, the function will never be called.
Computed properties can be accessed many times and often. Never use server calls in computed property code. Use caching if necessary.
Please note! Do not use arrow functions when defining computed properties. Arrow functions do not have their own this value (they use the parent context), so it will be impossible to get a reference to the object
In addition to the property read function (getter), you can also specify a set function (setter). This function will be called when an attempt is made to set a property value. Function arguments:
this
- a reference to the object to which the property is being added.value
- set value.Use properties with setters cautiously and carefully. When doing so, it is very easy to get infinite recursion.
An example of a document template with computing the amount for the row and the amount for the document. Suppose the model contains a document with rows, each of which has a price and a quantity. It is necessary to automatically calculate the amount for the row and the amount for the document.