A service provides a specific functionality. A service instance can be obtained by a ServiceFactory or at a Component via getService function.
This class is the abstract base class for services and needs to be extended:
sap.ui.define("my/Service", [ "sap/ui/core/service/Service" ], function(Service) { return Service.extend("my.Service", { init: function() { // handle init lifecycle }, exit: function() { // handle exit lifecycle }, doSomething: function() { // some functionality } }); });
A service instance will have a service context:
{ "scopeObject": oComponent, // the Component instance "scopeType": "component" // the stereotype of the scopeObject }
The service context can be retrieved with the function getContext
. This function is private to the service instance and will not be exposed via the service interface.
For consumers of the service it is recommended to provide the service instance only - as e.g. the getService function of the Component does. The service interface can be accessed via the getInterface
function.
Other private functions of the service instance are the lifecycle functions. Currently there are two lifecycle functions: init
and exit
. In addition the destroy
function will also by hidden to avoid the control of the service lifecycle for service interface consumers.
Creates a service for the given context.
Accepts an object literal mSettings
that defines initial property values, aggregated and associated objects as well as event handlers. See sap.ui.base.ManagedObject#constructor for a general description of the syntax of the settings object.
This class does not have its own settings, but all settings applicable to the base type sap.ui.base.Object can be used.
new sap.ui.core.service.Service(oServiceContext)
Param | Type | Default Value | Description |
---|---|---|---|
oServiceContext | object | Context for which the service is created |
|
scopeObject | object | Object that is in scope (e.g. component instance) |
|
scopeType | string | Type of object that is in scope (e.g. component, ...) |
Method | Description |
---|---|
destroy |
Lifecycle method to destroy the service instance. This function is not available on the service interface. |
exit |
Cleans up the service instance before destruction. Applications must not call this hook method directly, it is called by the framework when the service is destroyed. Subclasses of service should override this hook to implement any necessary clean-up. |
sap.ui.core.service.Service.extend |
Creates a new subclass of class sap.ui.core.service.Service with name
|
getContext |
Returns the context of the service: { "scopeObject": oComponent, // the Component instance "scopeType": "component", // the stereotype of the scopeObject "settings": {} // the provided service settings } This function is not available on the service interface. |
getInterface |
Returns the public interface of the service. By default, this filters the internal functions like This function is not available on the service interface. |
sap.ui.core.service.Service.getMetadata |
Returns a metadata object for class sap.ui.core.service.Service. |
init |
Initializes the service instance after creation. Applications must not call this hook method directly, it is called by the framework while the constructor of a service is executed. Subclasses of service should override this hook to implement any necessary initialization. |
Lifecycle method to destroy the service instance.
This function is not available on the service interface.
Cleans up the service instance before destruction.
Applications must not call this hook method directly, it is called by the framework when the service is destroyed.
Subclasses of service should override this hook to implement any necessary clean-up.
Creates a new subclass of class sap.ui.core.service.Service with name sClassName
and enriches it with the information contained in oClassInfo
.
oClassInfo
might contain the same kind of information as described in sap.ui.base.Object.extend.
Param | Type | DefaultValue | Description |
---|---|---|---|
sClassName | string |
Name of the class being created |
|
oClassInfo | object |
Object literal with information about the class |
|
FNMetaImpl | function |
Constructor function for the metadata object; if not given, it defaults to the metadata implementation used by this class |
Returns the context of the service:
{ "scopeObject": oComponent, // the Component instance "scopeType": "component", // the stereotype of the scopeObject "settings": {} // the provided service settings }
This function is not available on the service interface.
Returns the public interface of the service. By default, this filters the internal functions like getInterface
, getContext
and all other functions starting with "_". Additionally the lifecycle functions init
, exit
and destroy
will be filtered for the service interface. This function can be overridden in order to self-create a service interface.
This function is not available on the service interface.