If you find any errors, typos or have general feedback, select the text and click CTRL+ALT+ENTER.

Introduction

A Generic service is used to extend the standard REST service. It still has inputs and outputs (as a standard service), and it can be mapped, but the actual service implementation, or what it does, is entirely up to you. For example, the service can access local browser databases such IndexedDB or SQLite, or do calculations.

Creating a Generic service

A Generic service can be created via CREATE NEW > Service > Generic (custom JavaScript implementation):
CreateService

A new service will be created. Custom implementation can be added by clicking “Add custom implementation”:AddImplementation

Choose between using an existing JavaScript file or creating a new one:
CreateImplementation

Default or custom implementation can be opened by clicking “Open implementation”:
OpenImplementation

If you create a new JavaScript file, there’s default implementation code that can be edited by the user:
Code

Request & response parameters

Request parameters

As with the standard REST service, the Generic service also has Request and Response tabs. Any input/output parameters needed for custom implementation can be specified there. Input parameters can be accessed by settings.data.<parameterName>.

For example, a specified parameter with someParameter name and some_value value:RequestParam

This parameter can be accessed from the custom implementation JavaScript file with the following code:

The following custom implementation will simply log the input parameter:

In the example above, someParameter value was specified in the Request tab. It is also possible to pass this value from the page via components mapping.

For example; there’s a Textarea component on the page, where the user must input something. The following mapping will pass the someParameter with the value typed in the Textarea, to the custom implementation code:Mapping

 It doesn’t matter where the parameter value comes from; it can be accessed by settings.data.

Response parameters

To create the mapping with the results data, the Response tab must contain at least one parameter.

Lets say your custom implementation must return the username:Name

It’s possible to map the response to the component:ResponseMapping

The result can be specified in the implementation using the settings.success method. The parameter mapped to the page, the response parameter, and the parameter specified in the custom implementation must have the same name. In this case, it’s name:

Note: The response value must be always specified in JSON format.

Custom implementation for the example above:

Any number of parameters can be returned and mapped. The following screenshots show the implementation with name and age parameters in the response.

Response tab with two parameters: ResponseMapping2Params

Mapping two response values to different components:2ResponseValuesMapping

The implementation code in the case of two response values:

Response parameters will automatically be parsed and associated by names with the mapped parameters.

Settings

Class settings contains a few methods that works the same as with common REST Service. In the example above there are Before send method called firstly and lastly called the Complete method. These events are exactly the same that can you can see  from the visual builder:GenericService

If you need you can implement a GenericService implementation that will return Error in some case:

In this case service will call Error event and then Complete event. When you are mapping to an Error event there are 3 parameters available – jqXHR, textStatus and errorThrown:ErrorEvent

You can list this parameters separated by a comma as following:

Using local storage

With the help of local storage, any data can be saved and loaded from any service. The first service can save data into local storage, and the latter can load this data and use it.

Using local storage:

Executing another service

Executing another service within a generic service

Custom implementation allows you to support service calls from the JavaScript code. This is handy when you need to do some checks or advanced logic before calling the REST service.

Lets say there is a standard REST service that creates a new object in the todo collection.

Note: Read how to call services from JavaScript code here.

The following custom implementation simply invokes calling the add_todo_object service and passes some parameters to it. It can be easily extended with any logic you need: