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