Calling JS Service

Version added 2018-03-07 17:26:00| Modified 11-Apr-2018

Importing JS File

In webOS Open Source Edition, basic template contains the webOS library in its webOSjs-0.1.0 directory. You can add this library to your app as below:

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

If you import already existing web app, follow the 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 the 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 the source code. 

  3. Include the webOS library in your app as above.

Calling Services from Any Web App

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

To call a service in an app, 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
});

Calling Service from another Service

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

Below is an example of how a webOS service can be called using webOS.js.

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

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

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

 

Except as noted, this content is licensed under Creative Commons Attribution 4.0 and sample code is licensed under Apache License 2.0.