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 MashZone NextGen 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
MashZone NextGen 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 MashZone NextGen 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
MashZone NextGen 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
MashZone NextGen 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 MashZone NextGen Server when a user logged in. This is no longer needed. |
5. Create a request to invoke a MashZone NextGen mashable or mashup, supplying any 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:
<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
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. You can also request results in CSV or XML format.
There are many other
MashZone NextGen headers that your can use, however. See
MashZone NextGen
Headers/Parameters for a complete list.