Implementation of a custom service

Introduction

Here are the requirements to follow, whenever a plugin has to deal with a custom service:

  • The jsServer attribute MUST be present on the <es-plugin> tag of the declaration file.
  • The script for the server MUST be given (and valid).
  • The functions getLoadModelMessage() and getStoreModelMessage MUST be implemented.
  • The model is not managed by the system, so you have to manage it inside the plugin.
  • To make the save button available the method loadModel() has to be explicitly called.
  • To manage the serverResponse you have to implement the serverUpdate() method.
Except for the first two requirements, the other requirements are the same for using the API. The main difference between using the API and using a custom service is in the declaration of a JavaScript server. Furthermore, it is impossible for the plugin to manage both API and custom service calls in the same time. However, you can still call the API inside the custom service.

escmp.xml


<es-plugin
    name="CustomServiceDemo"
    layoutUsage="Job"
    smartviewUsage="Desktop,Job"
    jsServer="CustomServer.js">
 </es-plugin>

CustomServer.js


if(ESPluginParams.action == "myServerLoad") {
  var apiParams = {
    'id': 1,
    'method': 'job.get',
    'params': {
      'ID': ESPluginParams.ID
    }
  };
  ESPluginResponse = ESAPI.process(apiParams);
} else if(ESPluginParams.action == "myServerSave") {
  var apiParams = {
    'id': 2,
    'method': 'job.edit',
    'params': {
      'ID': ESPluginParams.ID,
      'description': ESPluginParams.description
    }
  };
  ESPluginResponse = ESAPI.process(apiParams);
}

Example


var JsServerDemo = {
    buildComponent: function() {
        return '<div>JsServerDemo</div>';
    },
    serverUpdate: function(response) {
        console.log(response);
    },
    update: function() {
        // Nothing done here
    },
    getLoadModelMessage: function() {
        var id = this.getModelProperty('ID');
        var params = {
            'action': 'myServerLoad',
            'ID': id,
        };
        var request = {params : params};
       return request;
    },
    getStoreModelMessage: function() {
        var id = this.getModelProperty('ID');
        var params = {
            'action': 'myServerSave',
            'ID': id,
            'description': 'JsServer edited!',
        };
        var request = {params : params};
        return request;
    }
};