0

I am trying to get scala.js working in conjunction with the w2ui jQuery library. However, when I define my reset action in a form, the behaviour is not as I would have expected.

In order to handle my reset action. I define something like the following in my scalajs code:

...
 actions = js.Dynamic.literal(
  reset = { form: W2Form =>
   form.clear()
  }: js.Function1[W2Form, Any],
...

However this causes an error when I click the reset button:

TypeError: this.refresh is not a function
this.refresh();
^

On examining the generated javascript code I can see why this error occurs:

"actions": {
  "reset": (function(f) {
    return (function() {
      return f.apply__O__O(this)
    })
  })(new $c_sjsr_AnonFunction1().init___sjs_js_Function1((function(form$2) {
    return (0, form$2["clear"])()
  }))),

This can be rectified changing the last line by hand to:

  return (form$2["clear"])()

Can someone tell me what I am doing wrong?

user79074
  • 4,937
  • 5
  • 29
  • 57
  • Hmm. I don't know w2ui, and the documentation isn't as well-organized as I might wish. Can you point to the docs for actions and reset, so I can understand what you're trying to do here? – Justin du Coeur Oct 29 '15 at 18:11

1 Answers1

2

Your usage of js.ThisFunction0 is perfectly fine. Your issue comes from your call to clear. I suspect you defined clear like this in W2Form:

@js.native
class W2Form(...) extends js.Object {
  val clear: js.Function0[Unit] = js.native // or var or def
  ...
}

That would cause form.clear() to first select the field clear from form, then calling the retrieved function independently from form. If you want clear to be called as a method of form, you need to declare it as a method:

def clear(): Unit = js.native
sjrd
  • 21,805
  • 2
  • 61
  • 91