0

When I define a spinner in ScalaJS and handle the spin value I am not able to get the new spin value in the event as I would have expected. According to the JQuery UI documentation the second parameter to the spin event is the ui object that contains a value attribute. So I defined a trait:

trait Number extends js.Object {
    val value: Int = js.native
}

And then handle my spin event thus:

jQuery("#mySpinner").spinner(js.Dynamic.literal(spin = { (e: HTMLInputElement, ui: Number)  =>
  log("Change: " + ui.value)
  }: js.ThisFunction1[HTMLInputElement, Number, Any]))

But the "value" attribute does not seem to be a member of the ui object as I get the exception below in my log statement. Can someone tell me what I am doing wrong?

uncaught exception: scala.scalajs.runtime.UndefinedBehaviorError: An undefined behavior was detected: undefined is not an instance of java.lang.Integer

user79074
  • 4,937
  • 5
  • 29
  • 57
  • Well obviously `ui` doesn't have a `value` attribute. You should log `ui` itself to see what its shape is. – sjrd Jul 07 '15 at 14:03
  • I did and it is an [Object] which is what it says in the JQuery UI documentation: http://api.jqueryui.com/spinner/#event-spin. But it should have a value attribute. How to extract this "value" Number is the question? – user79074 Jul 07 '15 at 14:31

2 Answers2

0

You say e: HTMLInputElement but it should be e: Event

martin-g
  • 17,243
  • 2
  • 23
  • 35
  • This is appropriate way to define a js.ThisFunction from what I have read. See: http://www.scala-js.org/doc/calling-javascript.html – user79074 Jul 08 '15 at 12:56
0

I suspect the problem is a combination of the previous comments. You are correct that, since you're using ThisFunction, the first element should be an Element of some sort. (Although, is it really an HTMLInputElement? That's a slightly unusual element type to put a spinner on.)

But that Element gets prepended to the function parameters, whereas you've got it replacing one.

In other words, you have (e: HTMLInputElement, ui: Number) but it needs to be (elem: HTMLInputElement, e:Event, ui: Number) in order to match the expected signature. So in practice, the system is trying to cast the value member of an Event, which of course doesn't exist, to Integer. It finds that value is undefined, tries to cast it to Integer, and boom.

I can't say I'm 100% certain (and IMO that ui parameter is just plain weird to begin with -- I'm a little suspicious of the jQueryUI documentation there), but that's my guess. Try fixing the signature of your call, and see if the error goes away...

Justin du Coeur
  • 2,699
  • 1
  • 14
  • 19