Calling JS Service

Importing JS File

From webOS TV 4.0, we provide webOSTV.js library instead of the webOS.js library.  Bootplate-web templates contain the webOSTV.js library in its webOSTVjs-1.1.0 directory. You can add this library to your app as below:

<script type="text/javascript" src="webOSTVjs-1.1.0/webOSTV.js"></script>

For more details about webOSTV.js, see the webOSTV.js guide. 

Calling Services from Any Web App

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

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 (Any Web App) for the code sample. 

Calling JS Services from Enyo 

Create an enyo.LunaService kind with the service information 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 the send method.

this.$.HelloWorldService.send();

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

Calling Service from another Service

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

Example of how a webOS service can be called using webOSTV.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", {}, function(response) {
        console.log(response.payload);
        if(response.payload.isInternetConnectionAvailable == true) {
            // ...
            message.respond({
                "returnValue": true
            });
        }
    });
});

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

Storing and Sharing Data
JavaScript services run inside a "jail," or the 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 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 (minutes) primarily because of the cost of memory.

  • Don't use the synchronous API from node.js 

Navigation