Creation and Usage of Common Functions - imona/tutorial GitHub Wiki
Imonacloud platform allows developers to create common functions for their applications to reuse (call) them from different components and from different entity screens. A common function's scope is the whole application.
To create a common function, follow Developer Tools - Create Function from an entity form or table screen.
In this screen, you will see the defined functions on the left and the editor on the right. You can create a new function by clicking Create New button or edit an existing function by clicking on the function on the left.
In the editor, you can write your function. Note that in an editor screen you can define only one function. These functions work the same as other scripts in platform, they use MVEL syntax and execute Java code. You can also use service methods of the platform and have content assist to help you.
A function's syntax should be like
function functionName(parameter1, parameter2) {
// codes, optional return statement etc
}
A sample function:
function saveAndInform(bean, message) {
if (bean == null) {
error("Bean should not be null!");
return null;
}
var result = save(bean);
if (message != null) {
info(message);
}
return result;
}
This function will be listed as saveAndInform on the left column of the Edit Functions popup.
Then you can call that function from any place in your application, i.e. from a button click script like:
//...
var myCustomer = create("customer");
myCustomer.status = "active";
myCustomer.creation_time = time();
var savedCustomer = saveAndInform(myCustomer, "Customer successfully saved!");
//...
Note that content assist (triggered by Ctrl + Space) will help you use defined common functions by suggestions.