I just did this recently and though it might come in handy for someone else.
First, you need to define a method in your component class (or container class, if using a container) to return the data the javascript will need. Your component and container can extend existing wdk components, I tried to keep my example simple.
The method should be as follows (you can give it any name):
public class MyComponent extends Component {
public void processRequest(Control control, ArgumentList args) {
String result = "";
Map responseData = new HashMap();
// update result string as needed
responseData.put(JSON_ATTRIBUTES, result);
setRedirectJsonRendererUrl(responseData);
}
public static final String JSON_ATTRIBUTES = "attributes";
}
If the component is using a container, the request is going to go to the container, so you need a similar method in the container. You can either put the method just in the container, or use the container to delegate the call to the component:
public class MyContainer extends DialogContainer
public void processRequest(Control control, ArgumentList args) {
Component comp = getContainedComponent();
if (comp instanceof MyComponent) {
((MyComponent)comp).processRequest(control, args);
}
}
}
Next, you need to add the asynchronous call in the javascript, either in the container's or the component's JSP:
<dmf:head>
<dmf:webform/>
<script>
function doInit() {
var prefs = InlineRequestEngine.getPreferences(InlineRequestType.JSON);
prefs.setCallback(callBack);
postInlineServerEvent(null, prefs, null, null, "processRequest", null, null);
}
function callBack(data) {
if (isEventPostingLocked()) {
releaseEventPostingLock();
}
if (data) {
var result = data['<%=MyComponent.JSON_ATTRIBUTES%>']
}
}
document.onload = doInit;
</script>
</dmf:head>
It's up to you how you want to invoke the asynchronous call, either using onload or when a user clicks something on the screen.
Message was edited by: Aashish Patil
Added the 'samplecode' tag