简体   繁体   English

GWT-JSNI在外部JS库中传递JavaScriptObject

[英]GWT-JSNI passing a JavaScriptObject in an external JS library

I'm desesperatly trying to create an Anno object in a JSNI method but I have a strange problem : the code in the jsni method doesn't work but if I do the same in my browser console, it works fine. 我疯狂地试图在JSNI方法中创建一个Anno对象,但我有一个奇怪的问题:jsni方法中的代码不起作用,但如果我在浏览器控制台中执行相同的操作,则可以正常工作。

Java Part Java部分

I'm using a JSONArray in wich I add some JSONObject (with all the elements, according to Anno doc). 我正在使用JSONArray ,我添加了一些JSONObject (包含所有元素,根据Anno doc)。 Here's my JSNI method : 这是我的JSNI方法:

// 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 浏览器部分

Just to be clear, the method is called on a onShow event, so all the ressources are loaded and rendered. 为了清楚onShow ,该方法在onShow事件上调用,因此所有的ressource都被加载和呈现。 So when the element is displayed and the function called, I have this error in my console : 因此,当显示元素并调用函数时,我在控制台中出现此错误:

Couldn't find Anno.target 'h1'. 找不到Anno.target'h1'。 --- anno.js:265 --- anno.js:265

NB : In Anno.js, h1 is the dafault value of target. 注意:在Anno.js中,h1是目标的dafault值。

But my steps value is right and when I do 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 the other. 我不明白为什么它在一个案例中起作用而在另一个案例中起作用。 I've also tried to use JSON.stringify then JSON.parse but it doesn't work either. 我也试过使用JSON.stringify然后JSON.parse但它也不起作用。


EDIT : 编辑:

I figured something out. 我想出了什么。 While debugging anno.js I figured something : when I initialized Anno in the console, the local scope look like this (bigger image here ) : 在调试anno.js时我想到了一些东西:当我在控制台中初始化Anno时,本地范围看起来像这样( 这里的图像更大):

控制台命令的本地范围

But when I use the jsni method, the local scope is absolutely different, my parameter is stored as an actual array instead of being processed normally (bigger image here ) : 但是当我使用jsni方法时,本地范围完全不同,我的参数存储为实际数组而不是正常处理( 此处更大的图像):

在此输入图像描述

The problem is that GWT code runs in an iframe (for sandboxing/isolation) and Anno only supports array types from the same browsing context. 问题是GWT代码在iframe中运行(用于沙盒/隔离),Anno仅支持来自相同浏览上下文的数组类型。

See http://perfectionkills.com/instanceof-considered-harmful-or-how-to-write-a-robust-isarray/ and http://web.mit.edu/jwalden/www/isArray.html for descriptions of the problem. 有关描述,请参见http://perfectionkills.com/instanceof-considered-harmful-or-how-to-write-a-robust-isarray/http://web.mit.edu/jwalden/www/isArray.html问题。

ECMAScript 5.1 added an Array.isArray() function that solves that issue and has wide browser support (back up to IE9): https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/isArray ECMAScript 5.1添加了一个Array.isArray()函数来解决这个问题并且具有广泛的浏览器支持(备份到IE9): https//developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/磁盘阵列/ IsArray的
And jQuery has one too, which makes it incomprehensible (to me) that Anno doesn't use it instead of using the broken if arg.__proto__ is Array.prototype (unless it's by design). 而且jQuery也有一个,这使得我不能理解Anno不使用它而不是使用破坏的if arg.__proto__ is Array.prototype (除非它是设计的)。 So first things first: file an issue on Anno. 首先要做的事情是:在Anno上提出一个问题。

As a workaround, it should be possible to use $wnd.Array.apply($wnd.Array, steps) to copy your array into an array from the top window. 作为一种解决方法,应该可以使用$wnd.Array.apply($wnd.Array, steps)将数组从顶部窗口复制到一个数组中。

It seems to me that the error message says it cant find the target dom element and not that it cant find the target property. 在我看来,错误消息说它无法找到目标dom元素,而不是它找不到目标属性。 Is the element there when your code is fired? 当您的代码被触发时,元素是否存在? make sure it is and get back to me. 确保它是,并回到我身边。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM