Search code examples
scala.jsscalajs-bundler

How to use jquery-ui with JSImport


I want to access the jquery ui library in my scala js project. I have tried defining the following main module:

import org.scalajs.jquery.JQueryStatic

import scala.scalajs.js
import org.scalajs.dom
import scalatags.JsDom.all._

import scala.scalajs.js.annotation.JSImport

@JSImport("jquery", JSImport.Namespace)
@js.native
object JQuery extends JQueryStatic

@js.native
trait JQueryUI extends JQueryStatic {
    def spinner(options: js.Object = js.Dynamic.literal()): JQueryUI = js.native
}

@JSImport("jquery-ui", JSImport.Namespace)
@js.native
object JQueryUI extends JQueryUI

object App {

    def main(args: Array[String]): Unit = {
        dom.document.getElementById("root").appendChild(div(input(id := "input")).render)
        JQuery("#input").asInstanceOf[JQueryUI].spinner()
    }
}

And my build.sbt is as follows:

enablePlugins(ScalaJSBundlerPlugin)

lazy val opexCounter = project.in(file(".")).settings(

    name := "Repro",
    scalaVersion := "2.12.8",

    libraryDependencies ++= Seq(
        "org.scala-js" %%% "scalajs-dom" % "0.9.6",
        "com.lihaoyi" %%% "scalatags" % "0.6.7",
        "be.doeraene" %%% "scalajs-jquery" % "0.9.4"
    ),

    npmDependencies in Compile ++= Seq(
        "jquery" -> "2.2.1",
        "jquery-ui" -> "1.12.1",
    ),

    mainClass in Compile := Some("App"),

    scalaJSUseMainModuleInitializer := true,

    webpackDevServerPort := 3000
)

But when I load my page I get the following error in my console:

TypeError: qual$1.spinner is not a function

Is this not the correct way to import the library and if not what is?

The complete source for the project can be found here


Solution

  • I changed my npm dependency from jquery-ui to jquery-ui-bundle and imported it. I also had to make an explicit reference to my JQueryUIImport object in order to ensure it was instantiated. Those 2 changes fixed the problem