After migrating from scala.js 0.6.x to 1.0, I've got some code related to @JSGlobalScope broken.
My use case is like this:
- there's a 3rd-party library that expects some global var to be set to a function
- when loaded and ready, it calls this function (by name)
- I set this function in global scope from scala.js
The code looks like this:
@js.native
@JSGlobalScope
object Globals extends js.Object {
var callbackFunctionFor3rdPartyLib: js.Function0[Unit] = js.native
}
then I set this var like this:
Globals.callbackFunctionFor3rdPartyLib = () => {
// do things
}
and then I add the script into the DOM.
This was working with scala.js 0.6.x, but with 1.0 I'm getting an exception like the following:
scala.scalajs.js.JavaScriptException: ReferenceError: callbackFunctionFor3rdPartyLib is not defined
In the changelog for 1.0.0 there's a "Breaking changes" section that mentions this:
Accessing a member that is not declared causes a ReferenceError to be thrown ...
js.Dynamic.global.globalVarThatDoesNotExist = 42would previously create said global variable. In Scala.js 1.x, it also throws a ReferenceError.
My question is:
what is the right way to do something like this (create a new global var) in scala.js 1.0?