Child pages
  • Angular services
Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 6 Next »

Services are similar to WebComponents except they have no user interface. They are mapped to plugins scope in order to call their api from scripting.

A service must contain the specification file and the js file. The service specification file is exactly the same as web component specification file. However, as there is no UI, there is no support for handlers.

Services implementations have to be present in special folders whose names end with "services" suffix (for example default servoy services are found into "servoyservices" directory). A folder that doesn't end with "services" will be handled as webcomponent folder.

An example service:

testservice.spec
{
	"name": "testservice",
	"displayName": "Test service that says helloworld",
	"definition": "servoyservices/testservice/testservice.js",
	"libraries": [],
	"model":
	{
		"text": "string"
	},
	"api":
	{
	 	"talk": {
	        }
	}
}

The service js file must define the api from the spec:

testservice.js
angular.module('testservice',['servoy'])
.factory("testservice",function($window,$services) {
	var state = $services.getServiceState('testservice');
	return {
		talk: function() {
			alert("talk: " + state.text);
			state.text = "something else"
		}
	}
})
.run(function($rootScope,$services)
{
	var scope = $rootScope.$new(true);
	scope.state = $services.getServiceState('testservice');
	scope.$watch('state', function(newvalue,oldvalue) {

		// handle state changes
	}, true);
})

From scripting, when calling plugins.testservice.talk() it should execute the service talk method. The service model is automatically synchronized with the server. In order to observe server side modifications the service must add a watch to the service state.

  • No labels