Friday, May 24, 2013

Re: java arrays in JSNI

Like explained in http://stackoverflow.com/a/6130473/1666829 you can call:

public static final native void trigger(String eventName, JsArray params)/*-{
        $wnd.mylib.trigger.apply(this, [eventName].concat(params)) ;
}-*/;

Pay attention to the second parameter which is a JsArray. You can convert it in e seperate method.

Am Dienstag, 15. Mai 2012 18:54:03 UTC+2 schrieb Sebastián Gurin:
Hi all. I'm trying to create a java API for a JavaScript function that can accept variable number of parameters, for ex. trigger("eventName", param1, param2, ...).

In java the desired java method signature, do not need to use java varargs, could be simple something like this:

public static final native void trigger(String eventName, Object[] params)/*-{
        ///$wnd.mylib.trigger(eventName, ....)
}-*/;


The thing is that the params array elements can be of any and mixed types. Could be POJOS or JavaScriptObject or java.lang.Integer.

My problem with this is that I don't know how to create a JsArray or a native JavaScript array and put all the elements of parameter object[]params.

I tried doing this in java using a JsArray and JsArrayMixed but elements must be casted to JavaScriptObject, and when doing so I get a "java.lang.Object cannot cast to com...JavaScriptObject" exception. This happens when trying to push an element into the jsarray: it need to be casted for java compiler: jsArray1.push((JavaScriptObject) myJavaArray[i]);

I also tried doing it in jsni javascript code, but I don't know how to access java arrays for obtaining its elements. The documentation says that from javascript java arrays are "opaque" objects. It also says :

"Although Java arrays are not directly usable in JavaScript, there are some helper classes that efficiently achieve a similar effect: JsArray,..... These classes are wrappers around a native JavaScript array."

But I think in my case, if I need to access a java array with java objects these classes have not use... ?

I think I'm missing something here... IMHO it is a common javascript technic defining functions with variable parameters ,and it sould be available to work with these from java using GWT JSNI. -

Any suggestion is mmost appreciated. Thanks in advance.

--
Sebastian Gurin <sgu...@softpoint.org>

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" 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 http://groups.google.com/group/google-web-toolkit?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

No comments:

Post a Comment