Overview of Widget API
The functions described here are typically used when working with code widgets, which further extend the functionality and flexibility of Yellowfin. These can be added to the dashboard by importing them through the plugin manager. They will then appear in the list of code widgets for selection during the dashboard building process. Take a look at the guide to creating a code widget for further information and resources.
Passing data with WidgetAPI.serverRequest and doRequest
At certain times during code widget development, you may need to pass data to the Yellowfin server. This can be achieved by using the WidgetAPI.serverRequest function. This makes a call to the Java class associated with the code widget.
When a request is made from the front-end component of a code widget, a CodeWidgetActionRequest object is created. This object contains the data which was added to that request.
This method will match the string that was passed to the WidgetAPI.serverRequest function. It should be used to determine which actions your code widget should be undertaking.
For example, when a request from the front end is made like this:
getAction() will return the string “hello”.
String getParameter(String parameterName)
When request.getParameter(“name”) is called, it will return “Code Widget”.
<T> getParameterAsObject(String parameterName, Class<T> clazz)
This function returns the value of the passed parameterName as the passed Class type. For example:
Calling the above code returns an integer. If the value cannot be parsed, null will be returned.
Or, for example,
When the request above is made from the front end, these can be retrieved directly as objects by doing the following in Java:
In this example, the variable counter will be parsed as an integer and MyClass will have the values mapped from the JSON to its corresponding private member variables.
Using this method has a limitation when generics are used, as type safety cannot be guaranteed. If you are attempting to parse an object using generics, we recommend you use getParameterAsObject(String, Type) instead.
<T> getParameterAsObject(String parameter, Type type)
This function attempts to parse the passed parameter key using the passed Type parameter.
This function works similarly to <T> getParameterAsObject(String parameterName, Class<T> clazz), but it lets you define a Type object, which is more suitable when using generics because you can use classes that support generics.
Using this function introduces a dependency on gson, which is shipped with Yellowfin, but is not documented on this wiki.
If you're not familiar with gson and wish to avoid using it, an alternative option is to get the String version and then parse it.
addData(String parameter, Object data)
The objects that are passed in can be much more complex than simple strings. Any Java class can be added to this. If you add an implementation of a Java class to any publicly-accessible getters, like getProperty or isProperty, they will be added to the JSON data. When this happens, the first letter after “get” will be changed to lowercase in the generated JSON. For example:
getProperty becomes property
isAProperty becomes aProperty
The following class:
...when added to the response:
...would create create the following object:
Promise - A promise that is resolved when the code widget request completes.
The promise will be passed an Object containing a JSON representation of the CodeWidgetResponse that is created by the backend component.
action - String - The action you wish the server to perform
actionData - Object - An object containing any data you wish to pass along with the request.