GWT-JSNI passes JavaScriptObject to external JS library

I am desesperatly trying to create an Anno object in a JSNI method, but I have a weird problem: the code in jsni method doesn't work, but if I do the same in my browser console, it works fine.

Java part

I am using JSONArray

which I am adding JSONObject

(with all items, according to Anno doc). Here's my JSNI method:

// I'm using the getJavaScriptObject() on my JSONArray
private static native void launch( JavaScriptObject steps )/*-{
        var anno = new $wnd.Anno(steps);
        anno.chainIndex().show();
                                                            }-*/;

      

Browser part

To be clear, the method is called on an event onShow

, so all ressources are loaded and displayed. So when the item is displayed and the function called, I have this error in my console:

Anno.target 'h1' could not be found. --- anno.js: 265

NB: In Anno.js, h1 is the dafault value for the target.

But my step values ​​are correct and when I execute the same commands in the console, it works:

var testAnno = new Anno([{
    content: "namespinnerFrequencyA",
    position: "center-right",
    target: ".dataAuto0"
},{
    content: "chooseFrequencyB",
    position: "top",
    target: ".dataAuto1"}]);
testAnno.show();

      

I don't understand why it works in one case and not in another. I also tried to use JSON.stringify

then JSON.parse

, but it doesn't work either.


EDIT:

I understood something. While debugging anno.js, I figured out something: when I initialized Anno in the console, the local scope looks like this (larger image here ):

local scope for the console commands

But when I use the jsni method the local scope is completely different, my parameter is stored as an actual array and not processed normally (larger image here ):

enter image description here

+3


source to share


2 answers


The problem is that the GWT code is executed in an iframe (for sandboxed / sandboxed) and Anno only supports array types from the same view context .

See http://perfectionkills.com/instanceof-considered-harmful-or-how-to-write-a-robust-isarray/ and http://web.mit.edu/jwalden/www/isArray.html for a description Problems.



ECMAScript 5.1 added a feature Array.isArray()

that addresses this issue and has broad browser support (back to IE9): https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/isArray And jQuery has one too, which makes it incomprehensible (to me) that Anno doesn't use it instead of using the broken one if arg.__proto__ is Array.prototype

(unless it's by design). So first things first: file an issue on Anno.

As a workaround, it should be possible to use $wnd.Array.apply($wnd.Array, steps)

to copy an array to an array from the top window.

+2


source


It seems to me that the error message says that it cannot find the target dom element, not that it cannot find the target property. Is the element there when your code runs? make sure it does and come back to me.



0


source







All Articles