Presto Development and APIs : Use Presto Connect for Javascript : Use PC4JS in Custom Apps
Use PC4JS in Custom Apps
With custom apps, PC4JS is a dependency of the Presto 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 Presto Server and Send Requests to Run Mashables or Mashups.
Get a Connection to the Presto Server
Use the getConnection() method to get the default PC4JS connection to the Presto 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 Presto 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 Presto mashables or mashups, supplying input parameters or optional Presto 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 Presto header passed as a parameter. You can define parameter values literally or programmatically. You can also use Presto attributes to have the Presto Server resolve the parameter value at run time. See Map Presto Attributes to Artifact Input Parameters for more information.
This example also uses one very common Presto header, x-p-resultFormat, as a parameter in the URL to ensure that the response is returned in JSON format. There are many other Presto headers that your can use, however. See Presto Headers/Parameters for a complete list.
See also Paginate Mashable or Mashup Responses.
Copyright © 2006-2015 Software AG, Darmstadt, Germany.

Product LogoContact Support   |   Community   |   Feedback