Saturday, August 12, 2017

Accessing "basic" JS Object in JsInterop callback

Hi all,
I have recently started testing JsInterop for our GWT 2.8.1 application and I have encountered a situation I could not solve without using the old style JSNI approach.

Consider the following minimal JS library I would like to use in Java/GWT:

(function (window, document, undefined) {

function WatchDog(name) {
   
this._name = name;
};

WatchDog.prototype.register = function(callback) {
   
this._callback = callback;
};

WatchDog.prototype.broadcast = function(msg) {
   
if (this._callback) {
       
this._callback({
           
'name' : this._name,
           
'timestamp' : new Date().getTime(),
           
'msg' : msg
       
});
   
}
};

window
.Dummy = {
    watchDog
: function(name) {
       
return new WatchDog(name);
   
}
};

}(window, document));

Please note that the broadcast function passes a "basic" JS Object {} (i.e. without a specific prototype) to the callback function.

Here below my JsInterop mappings:

package xxx;

import jsinterop.annotations.JsPackage;
import jsinterop.annotations.JsType;

@JsType(isNative = true, namespace = JsPackage.GLOBAL)
public class Dummy {
   
private Dummy() {}
   
public static native WatchDog watchDog(String name);
}

package xxx;

import jsinterop.annotations.JsMethod;
import jsinterop.annotations.JsType;

@JsType(isNative = true)
public class WatchDog {
   
private WatchDog() {}
   
@JsMethod
   
public native void register(Callback callback);
   
@JsMethod
   
public native void broadcast(String msg);
}

package xxx;

import jsinterop.annotations.JsFunction;

@JsFunction
public interface Callback {
   
void call(Object event);
}


In my Java code, when the callback function is invoked, the event parameter is an instance of com.google.gwt.core.client.JavaScriptObject.

Without using JSNI I am not able to access/read the object's properties. Does JsInterop addresses this scenario without falling-back to old style JSNI?

Thanks a lot in andvance
Andrea

--
You received this message because you are subscribed to the Google Groups "GWT Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-toolkit+unsubscribe@googlegroups.com.
To post to this group, send email to google-web-toolkit@googlegroups.com.
Visit this group at https://groups.google.com/group/google-web-toolkit.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment