Calling JS Service

Calling Services from Any Web App

Any application can include webOS.js and make webOS service calls using the webOS.service.request method.

 

In webOS TV SDK v2.0.0, bootplate-web template contains the webOS library in its webOSjs-0.1.0 directory. You can add this library in your app as below:

<script type="text/javascript" src="webOSjs-0.1.0/webOS.js"></script>

 

If you use webOS TV SDK v1.2.0 or import already existing web app, follow next steps: 

  1. Download the compressed library file from here. Then, unzip webOS.zip.
  2. In your app project, create a subfolder named webOSjs-0.1.0 and copy the downloaded webOS.js into lib folder. The webOS library file can be placed in any folder within your app project. You should set the proper directory when including the library in source code. 
  3. Include the webOS library in your app as above.

 

To call a service in an app developed without Enyo, use webOS.service.request.

var subscribeStatus = true; //change this to false to disable subscription 
var resubscribeStatus = true; //change this to false to disable resubscription 

var request = webOS.service.request("luna://com.mycom.helloworld/", {
	method:"someMethod",
	parameters: {  
		foo:"bar"  
	},
	onSuccess: function(inResponse) {  
		//....  
	},
	onFailure: function(inError) {  
		//....  
	},
	onComplete: function(inResponse) {  
		//....  
	},
	subscribe: subscribeStatus,
	resubscribe: resubscribeStatus
});

 

See HelloWorld Service (Enyo App) for the code sample.  

Calling JS Services from Enyo 

Create an enyo.LunaService kind with the information of the service that you want to use.

{
	name: "HelloWorldService",
	kind: "enyo.LunaService",
	service: "luna://com.mycom.helloworld/",
	method: "Hello",
	onComplete: "ServiceResponse"
}

 

Call the registered method by using send method.

this.$.HelloWorldService.send();

 

See HelloWorld Service (Any Web App) for the code sample. 

Calling Service from another Service

Any service can include webOS.js and make webOS service calls using the service.call method.

 

  1. Download the compressed library file from here. Then, unzip webOS.zip.
  2. In your app project, create a subfolder named lib and copy the downloaded webOS.js into lib folder. The webOS library file can be placed in any folder within your app project. You should set the proper directory when including the library in source code. 
  3. Include the webOS library.
<script type="text/javascript" src="lib/webOS.js"></script>

 

Example of how a webOS service can be called using webOS.js

var Service = require('webos-service'); var service = new Service("com.palm.service.helloworld");

service.register("hello", function(message) {
    service.call("luna://com.palm.connectionmanager/getstatus", {
        "params": {}
    }, function(response) {
        console.log(response.payload);
        if(response.payload.isInternetConnectionAvailable == true) {
            // ...
            message.respond({
                "returnValue": true
            });
        }
    });
});

 

For more detail about webos-service module, see webos-service API Reference.

Storing and Sharing Data
JavaScript services run inside a "jail", or permissions-restricted environment. Whenever a jailed service runs, it should always have the same user ID so that it can access files it wrote the last time it ran, but it can't access files created by other apps or services. If a service is expected to share its data with other apps or services, it must provide methods that will allow apps and other services to query and/or update its data. Data stored by a service should be saved to /media/internal.
 

Do Not

  • Services should not run for very long periods of time (minutes) primarily because of the cost in memory.
  • Don't use the synchronous api from node.js 

 

Navigation