Child pages
  • Server Side Scripting
Skip to end of metadata
Go to start of metadata

 

A component or service can have a serverside part, so logic is executed on the server, in the spec file this is configured like:

.spec file
"serverscript": "servoydefault/tabpanel/tabpanel_server.js",

Server side scripting should contain API methods defined in the spec of the component/service. When a certain API is called from Servoy scripting, Servoy will first check if that API is defined server side. If it is, it will just call the server side API method. If is not (or component doesn't have server side scripting) will send a message to client requesting an API call as defined by client js file. Any message sent to client will also send all outstanding model changes so that client will be in sync with server (before API is called).

That file has that also the implementation of the api that is defined in the spec file, so you can handle complex things server side, this can be used as a performance enhancement so that the api call doesn't have to go to the client to execute at the moment the call comes from servoy code.

The api call that is executed serverside could for example just update some models of the component/service, these will be then send as one thing when the servoy code is done.

Serverside code is build up the same as the clientside so you have $scope object with model,api and (since 8.3) handler objects. On that api object you have to define then the api methods that you want to execute server side. An example is the default tabpanel. More info on the $scope.handler object see below.

What can also be done (Servoy 8.0.2+) is that clientside scripting can call the server part. These function has to be defined on the $scope object, as an example:

 

serverside script
$scope.mycallback = function(name,type) {
	return "something";
}

Since Servoy 8.2 you must also define these functions in a special Api category of the spec, internalApi. In prior versions define these in api section instead.

serverside script
"internalApi":
	{
		"mycallback" :{
			"returns": "string",
	         "parameters":[
	            				{
						            "name":"name",
						            "type":"string"
					            },
					            {
						            "name":"type",
						            "type":"string"
						        }
	            			 ]
		}
	}

then a component can use the servoyapi to call this

client javascript
  // assign to the scope the svy-servoyapi
  scope: {
    	  model: '=svyModel',
		  servoyApi: '=svyServoyapi'
      },
 
// in the controller or link functions you can use that then
 $scope.servoyApi.callServerSideApi("mycallback",["test", "1"]).then(function(retValue) {
   console.log(retValue);    
 });

the callServerSideApi wants to have the function name and an array of arguments, it returns a promise of angular where the then function will give you the return value of the callback.

For a service you can use the $services service to do the same thing. the only thing extra is that you have to also give the servicename itself:

clientside service script
$services.callServerSideApi("myservicename","mycallback",["test", "1"]).then(function(retValue) {
   console.log(retValue);    
 });

Model changes to server

Icon

Beware that callServerSideApi does not send outstanding model changes to server, this should be handled by sending new values as parameters and modifying model server-side.

Internal API can also be used for API that is defined on client, but can only be called from server side scripting. This API shouldn't be called from Servoy Developer scripting and won't show in code completion.

Since Servoy 8.3 server side scripting also has access to the handlers a developer has assigned in the designer through $scope.handlers. Together with the new "private:true" configuration on a handler definition in the spec file, you can make handlers that are not directly callable from the client but only through server side scripting.

$scope.mycallback = function(name,type) {
    // call a hanlder directly with the arguments are return the value the handler gives
	return $scope.handlers.onAction(name,type);
}

Private handlers which are callable only serverside can be used to implement secure complex web components; for instance a Navigation Menu component which has a collection of MenuItems custom type. When the whole menu is disabled you can use the protecting properties (see Protecting Properties at Specification (.spec file)) to block certain handlers to be triggered; however when a single MenuItem is disabled and the navigation menu is enabled meanwhile, to securely prevent the handler to be executed for the disabled MenuItem (e.g. onMenuItemClick should be executed only if the menu item is enabled), you should call a server side script which verifies the MenuItem is actually enabled and triggers the private handler from the server side script.

Logging

Inside the server side scripting file of a component/service you can log messages using "console", not application,output. The output will appear in developer's console view as well as in the application server log file (depending on configured logging levels). For example:

Logging in component/service server side scripting file
console.log(message);
console.warn(message);
console.error(message);

Servoy API

Starting with Servoy 8.3.1 inside the server side scripting file of a component/service you can call specific api using object "servoyApi".

hideForm api  is used to mark a form as not visible anymore immediately because cannot wait for client to mark it as hidden(for example in removeTab of a tabpanel). 

servoy API example
servoyApi.hideForm('myform')

copyObject is used to create a new javascript object from existing one. This is a shortcut for creating the object from scratch and copying all properties.

servoy API example
var tabCopy = servoyApi.copyObject($scope.model.tabs[0])

getViewFoundSet is used to creates a view (read-only) foundset from a query builder select (QBSelect) (available starting with Servoy 8.4.0)

servoy API example
var query = parentFoundset.getQuery();
var myViewFoundset = servoyApi.getViewFoundSet("myViewFoundset", query);

getQuerySelect  is used to get select query for a dataSource (available starting with Servoy 2019.03)

servoy API example
var query = servoyApi.getQuerySelect(foundset.getDataSource());



  • No labels