Wiki

Imprimir Propiedades
Developer Guide

How to develop a new Operator

The first and second step should be implemented in the same file.

Implementing the operator

It is necessary to define a object of afrous.UnitAction class. This is an example, the Join operator which joins the element of an array into a String.
var myOperator = new afrous.UnitAction({
  type : 'Join',
  label : 'Join',
  description : 'Put all elements of the array into a string. Elements are separated by given delimiter.',
  inputs : [
    { name : 'array',
      label : 'array',
      type : 'String[]' },
    { name : 'delim',
      label : 'delimitor',
      type : 'String' }
  ]
  ,
  execute : function(request, callback) {
    var arr = request.params['array'] || [];
    var delim = request.params['delim'] || ',';
    callback.onSuccess(arr.join(delim));
  }
}))

The object has the next members:
  • type: the type of the operator
  • label: the label of the operator
  • description: a brief description of the operator.
  • inputs: The inputs of the form which will be shown in the GUI of MyCocktail
    • name: the name of the field in the form
    • field: the field of the field in the form
    • type: the type of input of the field
  • execute: this is the function which will be executed when the user press the "Refresh" button of the dialog.
    • How to retrieve the data of the fields? The value can be retrieved with this expression: request.params['labelOfTheField'] being labelOfTheField the label of the field that you want to retrieve.
    • How to return the value of the operation? Calling the next function with the return value as parameter:
callback.onSuccess(returnValue);

Registering the Operator in an UnitActionPackage

var myPackage = new afrous.UnitActionPackage('Basic');
myPackage.register(myOperator);
afrous.packages.register(myPackage,<filename.js>);
In the first line the package is created and, in the second one the operator is registered. In the third the package is registered in Afrous, the filename must be the file with the operator (without any path if registration is made in the same file).

Registering the JS file in order to be loaded with MyCocktail

If you put the code in the file myPackage.js you have to register this file in the application, editing the file /web/js/afrous/afrous-stdlib-index.js. You have to include the next line:
 p.loadScript(u+'/myPackage.js'); 
The default folder is /web/js/afrous, so if you put the JS file in other folder you have to indicate to MyCocktail.
1441 Accesos, 0 Ficheros adjuntos 0 Ficheros adjuntos

  • Comentarios