custom component with publishing API to invoke from javascript

Hi,

I am trying to create a custom component, in the same I wanted to publish an API to invoke it from external javascript. On the client side class VComponent.java, I have below methods:

public void refresh() {
System.out.println(“Update method called…”);
client.updateVariable(id, “r”, true, true);
client.sendPendingVariableChanges();
}

public static native void setUpAPI() /*-{
	var _this = this;
	$wnd._refresh = function(){
		_this.@com.gehcit.ehealth.cdt.gwt.client.VRefresher::refresh()();
	}
}-*/;

I am invoking the refresh() method from JSNI method setUpAPI(). As per the GWT documentation, this should expose (or publish) _refresh() javascript function that can be invoked from any html or javascript on the same page. But this is not working? Am I doing anything wrong?

Thanks,
Trivedi

Sounds like you have the right approach, but when are you calling the setUpAPI method? Typically call it from the widget constructor so it is available initially to any JS running after that.

Thank you for the quick reply.

As per your suggestion, I added method calling in widget constructor. Looks like that is working, tested by adding alert() method like below:

public static native void setUpAPI() /-{
var _this = this;
$wnd._refresh = function(){
alert(‘from custom widget’);
_this.@com.gehcit.ehealth.cdt.gwt.client.VRefresher::refresh()();
}
}-
/;

But the second statement “_this.@com.gehcit.ehealth.cdt.gwt.client.VRefresher::refresh()();” is supposed to invoke refresh() method in the same class, which is not getting invoked at all. Is the syntax right?

Here is my VRefresher.java class:

My server side component class is:

My requirement is to invoke refresh() on VRefresh.java from external javascript or html file in the same page. Please help me.

thanks,
Trivedi

Small things that I noticed. I don’t know if one (or any) of these is the problem, but might check them:

  • client.sendPendingVariableChanges should not be needed as the updateVariable is immediate
  • I’m not sure how the System.out works in GWT / client-side. You might as well remove this.
  • You are missing the changeVariables handling code. Like

    if (variables.containsKey(“r”)) {
    boolean r = (Boolean)variables.get(“r”);
    /* do something maybe, and call requestRepaint() */
    }

Anyways, if you are just trying to refresh and read updates from the server, there is already registered function for this in JavaScript
vaadin.forceSync()
that does this. (Unfortunately I could not find proper documentation about this, but
Book of Vaadin 11.5.1
mentions about it)

Hi Sami Ekblad,

Thank you so much for the providing javascript API vaadin.forceSync(), which is what I am actually looking for.

You have really made my day!!!

Really appreciate for your prompt responses.

Thanks,
Trivedi