Sunday, August 20, 2017

Re: Accessing "basic" JS Object in JsInterop callback


regarding 1) It would be nice to initialize Objects from json. What would happen if the json text specifies members not in class though?

You could always do this, if you just need to pass something to JavaScript and do not need to read it...

@JsType(isNative=true, name="JSON", namespace=@JsPackage.GLOBAL)  public class JSON {     Object parse(String json);  }  ....  Object config=JSON.parse("{foo:1, bar: ["a","b","c"], baz: {a: true, b: false}}");  
You c​
You could even do this if Config is a JsType with name Object and namespace GLOBAL:

Config config=(Config) JSON.parse("{foo:1, bar: ["a","b","c"], baz: {a: true, b: false}}");

In that case, any properties that do not exist in Config would simply not be readable or writable from your Java code, without using JSNI...




On Sunday, August 13, 2017 at 10:46:59 AM UTC-5, Vassilis Virvilis wrote:

I had this problem with DataTables Config. DataTables is a nice javascript library for tables https://datatables.net/ and Config is the initialization object like yours. So here is what I did:

    @JsType(isNative = true, namespace = JsPackage.GLOBAL, name = "Object")
    public static class Config {
        String name;
        TimeStamp timestamp;   // TimeStamp needs to be jsinteropified too or elementat2 needs to be used.
        String msg;
 
        @JsOverlay
        public static Config create(String name, TimeStamp timestamp, String msg) {
               final Config config = new Config()
               config.name = name;
               config.timestamp = timestamp;
               config.msg = msg;
        }
 }

Hope that helps.

1) It would be nice to initialize Objects from json. What would happen if the json text specifies members not in class though?

2) It would be interesting if JavaScriptObject was below Object in the GWT inheritance and expose .__set() for all java objects. I don't know if that is is possible but I find the thought intriguing. It would be break javac compilation though...

    Vassilis




On Sat, Aug 12, 2017 at 11:59 AM, Andrea Martino <cia...@gmail.com> wrote:
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




--
Vassilis Virvilis

--
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