NathanL
NathanL

Reputation: 81

Calling a js.ThisFunction elegantly from Scala.js

The TypeScript definition for mapbox-gl-js specifies that the loaded method of the Map class return a boolean

loaded():boolean     //def loaded:Boolean = js.native in Scala.js

However, the method actually returns the following JavaScript function, which in turn returns the boolean expected.

function(){
  return !this._styleDirty&&!this._sourcesDirty&&!!this.style&&this.style.loaded()
}

This made me changed its return type to js.ThisFunction,

def loaded: js.ThisFunction = js.native

,called it with the current instance of the Map class named map, and then converted the resulting js.Dynamic to Boolean,

map.loaded.call(map).asInstanceOf[Boolean] //Passing in map as the execution context (this)

It works, albeit quite cumbersome. Any way to do it elegantly? Thanks!

Upvotes: 1

Views: 112

Answers (1)

sjrd
sjrd

Reputation: 22085

Regardless of the implementation, I would recommend to define the method as

def loaded(): Boolean = js.native

to begin with. Then you can call it as

map.loaded()

If you really want to make the ThisFunction explicit, you should at least use a typed one:

def loaded: js.ThisFunction0[Map, Boolean] = js.native

and then you can call it as

map.loaded(map)

Upvotes: 3

Related Questions