JavaScript APIs that can be used in views
Use the following reference information to learn about JavaScript APIs that can be used in views.
The following view APIs and scenario are specific to Application Designer. For a list of view APIs that are shared between the Application Designer and Process Designer in the traditional workflow capabilities, see API reference.
If you need to make an xhr request from your view to other offering services, due to the cross-domain security protection, you need to rewrite your xhr connection string by using the rewriteURI method that is available in the view context object. The rewriteURI appends the connection string with Application Engine Proxy connection information. When you send the xhr request from your view with the updated connection string, the request is sent to the Application Engine Proxy. The proxy looks up the application resource for your offering, and then routes your request to the offering service. You need to define an application resource for the offering service and pass the name of the application resource to the rewriteURI API. If the application resource is not defined or found, Application Engine Proxy does not route the request and returns an error to your view.
Additionally, an application that displays documents can use getAddressableUrl to provide the user with a shareable link to display the current document. Then, subscribeAppState can be used automatically display that document when the shared link is used.
Function | Description |
---|---|
context.rewriteURI(uri, resourceName, resourceKeySuffix) | This API returns a proxy URI string for making
REST API calls to a backend server. If a proxy or resourceName is
not defined, the original URI is returned.
|
context.getCSRFTokenHeaderName() | Returns the HTTP header name for cross site request forgery. |
context.getCSRFToken() | Returns the cross site request forgery token value to be used in all future HTTP requests by setting the header if this value is not "undefined". |
context.loadFile(file, callback) | Handles loading of a file into a DocumentFile business
object.
|
context.getAddressableUrl(appState, withPageId) | Returns the application's URL with the optional
application state added to the URL. If the user returns to the application
by using the URL, they are return to the starting page of the application.
|
context.subscribeAppState(callback) | Registers a callback function to receive a notification
that is associated with the application state. A callback function
should be registered in a view's load or view event handler.
|
context.isWorkflowPXServer() | Checks whether the server is a Workflow Process Service Server. For the view in your application, this always returns false. |
Sample: Connect to Watson Translation Service
- Create a "Defined Endpoint" application resource; for example, watson. Then, add host, port, and authentication information to the application resource.
- Use the following sample code to send xhr request in your view.
var pillarStr = "watson"; var connectionStr = "/language-translator/api/v3/translate?version=2018-05-01"; connectionStr = this.context.rewriteURI(connectionStr, pillarStr); xhr.open("POST", connectionStr, true); //Send the proper header information along with the request xhr.setRequestHeader("Content-Type", "application/json"); xhr.setRequestHeader("Accept", "application/json"); // set CSRF Token xhr.setRequestHeader(this.context.getCSRFTokenHeaderName(), this.context.getCSRFToken()); var _this = this; xhr.onreadystatechange = function () { if (xhr.readyState === 4 && xhr.status === 200) { var json = JSON.parse(xhr.responseText); console.log("response translation: ", json.translations[0].translation); console.log("response: ", json); } else { console.log("post request problem: " + xhr.readyState); console.log("post request problem: " + xhr.status); } }; var senddata = {text: this.context.binding.get("value"), source: this.context.options.fromLang.get("value").name, target: this.context.options.toLang.get("value").name }; // Add JSON body xhr.send(JSON.stringify(senddata));