Important Notice - Forums is archived
To simplify things and help our users to be more productive, we have archived the current forum and focus our efforts on helping developers on Stack Overflow. You can post new questions on Stack Overflow or join our Discord channel.

Vaadin lets you build secure, UX-first PWAs entirely in Java.
Free ebook & tutorial.
JavaScript - Reference not defined
Hello,
I want to use a JavaScript library inside an addon that I am currently working on.
I added the according @JavaScript annotation on my AbstractJavaScriptExtension (it is going to be an extension, not a component):
@JavaScript({"vaadin://addons/js/mylib.js"})
public class MyExtension extends AbstractJavaScriptExtension {
}
The library is located inside src/main/resources/VAADIN/addons/mylib.js.
Now inside my connector I want to use the library inside the extend() method that is calling a JSNI method:
@Connect(MyExtension.class)
public class MyExtensionConnector extends AbstractExtensionConnector {
@Override
protected void extend(ServerConnector target) {
callJsniMethod();
}
private native void callJsniMethod() /*-{
var myVar = new $wnd.MyLib.MyClass();
}-*/;
}
Now by using the console inside the Chrome Developer Tools I can see that I get an error that "MyLib" is not defined at the moment of execution (I set a breakpoint).
Later on, once the page is fully loaded, I can create an instance inside the console.
Am I doing something wrong or is the library loaded too late or whats going on here?
Thank you already for your help!
Ok, I found out a way to mak it work:
In the script a global vairable was set using:
let MyVar = new MyLib;
It seems that this is not working because the script is loaded/executed in a non global scope by vaadin.
That way the global object does not exist and is not accessible.
Maybe a vaadin guy could confirm this?