BasicService and Service
When looking at how Enyo works with services--processes whose responses are available asynchronously--a good place to start is with BasicService and its subkind, Service.
BasicService
Applications will not typically create instances of BasicService, but rather of Service or a descendant of Service. Still, it's instructive to examine BasicService, since this kind contains the low-level plumbing common to all Enyo service kinds.
Like all Enyo services, a BasicService manages interaction with an asynchronous process. When the process is complete, the onResponse event, containing the service response, fires. If the response was successful, the onSuccess event fires; if not, the onFailure event fires.
A BasicService is initiated by calling the request method. Any properties relevant to the request should be passed to this method in the inProps object.
Note that a BasicService makes no assumptions about the actual asynchronous process. Instead, the request method creates an enyo.Request component, which manages the service request. (This component is, in fact, the return value of the request call.) You may specify the kind for the request component by setting the requestKind property of the inProps object.
Here's an example of a BasicService:
{kind: "BasicService", onResponse: "serviceResponse"}
To initiate the service and handle the response, do the following:
buttonClick: function() { this.$.basicService.request({index: 27, message: ""}); }, serviceResponse: function(inSender, inResponse, inRequest) { // process response }
To cancel all requests:
this.$.basicService.cancel();
Published Properties
| Name | Default | Description |
| service | "" | The name of the service. This information is delegated to the Request component. |
| timeout | 0 |
The timeout, specified in milliseconds, after which the service should return a failure condition. If a request times out, the request object will have its didTimeout property set to true.
|
Service
Service extends BasicService, adding support for making multiple request calls. Use the call method (rather than request) to initiate a Service call.
The call method takes two arguments. The first, inParams, is an object containing the parameters the request should use to make the service request. The optional second argument, inProps, is an object whose fields specify values for the properties of the request itself. (These properties may include onSuccess, onFailure, onResponse, and name.)
Note: Event handler delegate names are most commonly passed via call.
Here's a simple service declaration:
{kind: "Service", service: "importantService", onResponse: "genericResponse"}
This service could be called in a variety of ways, e.g.:
this.$.service.call({index: 5});
In the following case, the genericResponse method will be called with the service response:
this.$.service.call({index: 10}, {onResponse: ""});
To cancel a specific service call request, use cancelCall(inName), where inName is the name of the service call to cancel.
Finally, note that Enyo includes several subkinds of Service--e.g., PalmService, DbService, and WebService--that are useful for working with specific types of services.