Calling services from views
You can invoke service flows from views. The coach framework calls the services by using
workflow REST APIs in taskless mode.
Before you begin
About this task
To call a service in a view, you must specify configuration options of type Service in the view variable declarations and select a service flow as a default service to be used. The default service is the API for which custom services must match. The names and types of both inputs and outputs must match.
You can implement the service using either a simple JavaScript call syntax or a REST API.
Procedure
Example
The following example is JavaScript code for a
load
event handler:var _this = this;
var input = {text: this.context.options.service_option_name.get("value")};
var serviceArgs = {
params: JSON.stringify(input),
load: function(data) {
console.log("service returned: ", data);
// now dynamically create the img tag
require(["dojo/_base/url"], function(url) {
var relPath = new url(data.path).path;
domConstruct.create("img", {src:relPath, style:"margin:5px 0px"}, _this.context.element, "first");
});
},
error: function(e) {console.log("service call failed: ", e);}
}
this.context.options.service_name(serviceArgs);
Tip: If the service output is a complex-type business object, the data object that you
get from the response contains a property holding the metadata of your object, for example:
{"status":"200","data":{"serviceStatus":"end","key":"@54","step":"End","data":
{"bookPlacedPosition":{"Floor":1,"Room":"101","Row":2,"@metadata":
{"dirty":true,"shared":false,"rootVersionContextID":"2064.c30905ba-8d17-41f4-
b2a8-08cbb6516ff0T","className":"PlacedPosition"}}},"actions":null}}
If you directly set
the response object to your binding like the following example, the @metadata
object is added in your structure:
this.context.binding.get("value").set("BookPlacedPosition",data.bookPlacedPosition);
When
you trigger the boundary event to the server, the server throws an exception because it does not
expect the boundary event to have the @metadata
object. To avoid an exception,
remove the @metadata
object from the response before you set it to the binding, for
example:delete data.bookPlacedPosition['@metadata'];
_this.context.binding.get("value").set("BookPlacedPosition",data.bookPlacedPosition);