Use PC4JS in Custom Apps
With custom apps,
PC4JS is a dependency of the
Business Analytics App API so it is loaded and available automatically. You do not need to add a
<require> declaration in the App Spec. Simply
Get a Connection to the Business Analytics
Server and
Send Requests to Run Mashables or Mashups.
Get a Connection to the Business Analytics Server
Use the getConnection() method to get the default PC4JS connection to the Business Analytics Server that also hosts this app.
For example:
Presto.namespace("MyOrg");
var MyOrg.MyCustomApp = function(app){
var root = jQuery( app.rootElement ).find( '#content' );
var connection = app.getConnection();
...
}
In rare cases, you may need to get a connection to a remote Business Analytics Server (any server other than the server hosting this app). Use PC4JS to create another connection in this case, such as:
Presto.namespace("MyOrg");
var MyOrg.MyCustomApp = function(app){
var root = jQuery( app.rootElement ).find( '#content' );
var local = app.getConnection();
var remoteUrl = "http://my.other.domain:8080/presto";
var remote = new Presto.Connection({prestoUrl: remoteUrl});
...
}
In most cases, you do not need to login or provide user credentials for the connection. See
Guest or Authenticated Access with PC4JS to determine authentication requirements.
Send Requests to Run Mashables or Mashups
Once you have a connection, you create requests to invoke Business Analytics mashables or mashups, supplying 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:
...
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 invokeFor 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. There are many other
Business Analytics headers that your can use, however. See
Business Analytics
Headers/Parameters for a complete list.