OpenWGA 7.6 - OpenWGA Concepts and Features

Design and development » HDBModel framework » HDBModel in TMLScript » Event scripts » Defining event scripts

Simple (non-hierarchical) event scripts

Simple event scripts are available for both, content documents and storage documents.

There are two simple event scripts which can be defined:

onCreate gets called once a content of the given content class or a storage of the given storage id is created. The event is executed...
  • after the creation (obviously)
  • after HDBModel has initialized optional item defaults
  • after the data of an optional WebTML form that was given to the operation has been transferred
  • after an optional update process ran
  • but before any saving operation (unless the update process did one, obviously)

The purpose of onCreate therefor is to perform any initialisation actions on newly created contents and storages that need to be done programmatically. They for example could import some data from the parent content:

this.onCreate = function(e) {

  content().setItemValue("status", context("relation:parent-project").defaultStatus);

}

A frequent use case for of event scripts for storage documents is to prevent the storage from being visible on any navigator:

this.onCreate = function(e) {

  content().setHiddenFrom([content().DISPLAYTYPE_NAVIGATOR]);

}

onSave gets called every time a content of the given content class or a storage of the given storage id is saved. The event is executed.

  • before the data is actually saved to disk
  • but may be called multiple times on create/update processes. For example: Creating a content actually saves the document two times: Once after all data has been set on it and once after it has been published. This is due to the processes that run on the underlying WGAPI operations. So when onSave gets called it is sometimes possible that the current data has actually already been saved to disk by some earlier saving.

The purpose of onSave may be to:

  • Validate data modifications coming in from WebTML form or update processes. It is the right to determine if the document in its current status should actually get saved or is in invalid state.  if the script determines that it should not it should call the method hdbModelEvent.cancel() which cancels the operation and prevents persisting the changes:

this.onSave = function(e) {

  if (status == 'closed' || status == 'progress') {

    if (context("relation:assignedto", false) == null) {

e.cancel("You must assign the issue to a developer in this status");

    }

  }

}
  • Calculate derived data that should get stored on the  document as item or metadata. If for example the title of your content document should be built up from the data of one or multiple items then the onSave event is the place to calculate and set this title. For example, taking the title from the items "name" and "status":

this.onSave = function(e) {

  content().setTitle(name + "( " + status + ")");

}

As you see above, both simple event scripts onCreate and onSave run in the context of the created/saved document. Therefor you are able to directly read items and metadata in short form, also to follow relations of the document using context expressions.

Also both scripts should do no save operation on the created content itself. From their perspective saving happens implicitly.