A2v10 Platform documentation
UK


Templates. Events

The object describing events in the template is a regular JavaScript object. The property names of this object are the names of the events, and the values are the functions that will be called when the event occurs.

Event names are constructed in different ways depending on the type of event:

  • the names are fixed for the entire model.
  • for the event of object creation they are constructed from the object type and .constuct suffix.
  • for data change events ― from the full path to the data item and the specified suffix.

The full path to the item actually represents the fully qualified name of the object's property, starting from the root of the model. The [] suffix is used to indicate an array item.

Syntax:


Events for the entire model

Those events have fixed names.

  • Model.load - model is downloaded.
  • Model.unload - model is uploaded. No action is possible anymore. Most often used to unsubscribe from events on the global event bus.
  • Model.saved - model is saved.
  • Model.beforeSave - before saving.

Arguments for the model events:

  • this - data model root (IRoot).
  • model - data model root (IRoot).
  • caller - reference to the data model root (IRoot) of the caller. For example, if this is a dialog model, then the caller will be a reference to the model from which this dialog was called. Can be used to interact with the caller. For Model.saved, Model.save events, the value will always be undefined.

Events for objects

  • .construct - the object is created. The event name prefix represents the name of the object type. In the handler of this event, you can add new properties to the created item. However, it is better to use the (properites) mechanism for this purpose.
  • .change - property value has been changed. The event name prefix is the full path to the property in the data model.
  • .changing - the property value will be changed. The event name prefix is the full path to the property in the data model. It is possible to undo the changes.

Arguments for the .construct event:

  • this - data model root (IRoot).
  • elem - created item.
  • prop - the name of the property of the parent item to which the created object will be assigned. Makes sense for the items creating handlers in a MapObject.
Arguments for the .changing, .change:
  • this - data model root (IRoot).
  • elem - item.
  • newValue - the new value of the property.
  • oldValue - the old value of the property.
  • prop - the name of the property to change.
The returned value only makes sense for the .changing event:
  • Boolean - strict false value. Cancels an attempt to change the property value. Any other value allows changes.
Please note! When changing the values of object properties, both property values (old and new) will point to the same object. This is because the object is never re-created. Its properties are simply overwritten.

Events for arrays

The name prefix for all array events is the full path to the property in the model.

  • [].adding - before adding the item to the array.
  • [].add - the item is added to the array.
  • [].change - the array item has been changed.
  • [].remove - the item has been removed from the array.
  • [].select - the array item is selected.

Please note! The events will be generated only when platform tools are used to manipulate the arrays. If the array is changed using standard methods (for example, push, splice), then no events will be generated.

Arguments for the array events:

  • this - data model root (IRoot).
  • arr - modifiable array.
  • elem - new or deleted item.
The returned value is only meaningful for the .adding event.
  • Boolean - strict false value. Cancels the operation of adding an item to the array.

User-Defined Event

The events described above are inline events. However, no one forbids the use of custom events, which can more accurately describe the business logic of the application.

Such events are generated by the $emit method of the root object (IRoot) and can have an arbitrary name (of course, not overlapping with system names). The $emit method can also generate system events.

Please note! The event model in the system is associated with data, not with elements of the user interface. In other words, events occur when data changes. The occurrence of events bears no relation to the user interface. In fact, there may be no user interface at all.