Business Analytics Development and APIs : Use Business Analytics Connect for Javascript : Use PC4JS in Other Web Applications or Web Sites
Use PC4JS in Other Web Applications or Web Sites
You can use PC4JS outside of custom apps, for other web applicatons or web sites following these steps:
1. Include the Business Analytics Library Loader in pages that use PC4JS to invoke mashables or mashups:
<script type="text/javascript" src="app-server:port/presto/hub/jsapi/loader.js"/>
The Libary Loader API handles loading all of the libraries and dependencies for PC4JS and any other library installed with Business Analytics that you want to use.
2. Load the PC4JS library using the Presto.loadLib method in Library Loader:
<html>
<head>
<script src="http://localhost:8080/presto/hub/jsapi/loader.js"
type="text/javascript"></script>
<script>
Presto.loadLib("presto-core", null, true);
...
</script>
</head>
...
Use this method to load any additional configured libraries that you need. Or use other Library Loader methods to load CSS stylesheets or other JavaScript libraries.
3. Define the URL to the Business Analytics Server hosting the mashables or mashups you want to work with.
//local connection to same app server
prestoUrl = "/presto";
//remote connection
var prestoUrl = "http://204.10.32.210:8080/presto";
If you omit the URL, this defaults to the local host using the default Tomcat port ( http://localhost:8080/presto) at runtime.
4. Create a new connection passing a configuration object including:
*prestoURL: with the local or remote URL to Business Analytics defined previously. For example:
var connection = new Presto.Connection({prestoUrl: prestoUrl});
*username and password: these properties are only required if the connection must be authenticated and Business Analytics configuration does not handle authentication automatically. See Guest or Authenticated Access with PC4JS to determine whether you need to add credentials to the connection.
For example:
var connection = new Presto.Connection({prestoUrl: ’/presto’, username: 'aUser', password: 'myPassword'});
Note:  
In previous releases, PC4JS allowed connections to use an authToken obtained from the Business Analytics Server when a user logged in. This is no longer needed.
5. Create a request to invoke a Business Analytics mashable or mashup, supplying any input parameters or optional Business Analytics headers as needed.
You can find a sample of the code to invoke a specific mashable or mashup in the Technical Spec tab on the mashable’s or mashup’s artifact page. See Use Mashable/Mashup Technical Specs for more information.
You define a configuration object with properties for the request and provide callbacks for success and failure responses. A simple example would look something like this:
<script>
var requestBody = '';
//local connection
var connection = new Presto.Connection({ prestoUrl: "/presto" });

connection.request({
url: "/presto/edge/api/rest/YahooWeatherREST/getData?x-presto-resultFormat=json&p=94102",
type: "get",
contentType: "application/x-www-form-urlencoded",
data: requestBody
},
{ onSuccess: function(response, responseHeaders) {
var result = response;
if(typeof response !== "string") {
result = Object.toJSON(response);
}
//handle response data
},
onFailure: function(e) {
//handle error message
}
});
</script>
The url property identifies the mashable or mashup and operation to invoke. For more information on request configuration properties, see the PC4JS Reference.
The URL can also contain input parameters for the operation and any Business Analytics header passed as a parameter. You can define parameter values literally or programmatically. You can also use Business Analytics attributes to have the Business Analytics Server resolve the parameter value at run time. See Map Business Analytics Attributes to Artifact Input Parameters for more information.
This example also uses one very common Business Analytics header, x-p-resultFormat, as a parameter in the URL to ensure that the response is returned in JSON format. You can also request results in CSV or XML format.
There are many other Business Analytics headers that your can use, however. See Business Analytics Headers/Parameters for a complete list.
See also Paginate Mashable or Mashup Responses.
Copyright © 2013-2016 Software AG, Darmstadt, Germany.

Product LogoContact Support   |   Community   |   Feedback