GWT supports several wrappers for JavaScript arrays, e.g.
JsArray,
JsArrayNumber and
JsArrayInteger. However, they all require JavaScript primitives or
JavaScriptObjects. I needed to pass a JavaScript array containing regular GWT objects to a third party JavaScript library, together with a callback function that resolves specific properties for elements in the array. The skeleton of the array is similar to the JsArray classes provided by GWT (
full source code):
import com.google.gwt.core.client.JavaScriptObject;
public class JsGenericArray<T> extends JavaScriptObject {
// typical JsArray methods
}
I initially tried to use this array with the following factory method:
public static native <T> JsGenericArray<T> createGenericArray() /*-{
return [];
}-*/;
However, I ran into the problem that
instanceof Array
tests on this array in the 3rd party libraries returned false (as debugging showed). This is because
GWT runs in an iframe and instanceof does not work across frames. One recommended solution is
duck typing, but this was not an option because I did not want to change the 3rd party JavaScript library. To address this issue, I changed the factory method to return an array object from the main frame in which the JavaScript libraries run:
public static native <T> JsGenericArray<T> createGenericArray() /*-{
return new $wnd.Array();
}-*/;
This approach worked with Firefox 3.6 and Chrome 9, but not with Safari (see
Webkit Bug 17250). However, I am not a JavaScript expert and would appreciate any feedback on potential shortcomings of this solution, especially concerning cross-browser compatibility.