I know that I can run a script submitting an ajaxText this way:

SHtml.ajaxText(myVar, (str) => myVar = str, "onsubmit" -> "myScript();")

What I would like to do is to execute a scala method instead.

Obviously

SHtml.ajaxText(myVar, (str) => myVar = str, "onsubmit" -> myMethod)

is not working.

Is there a way I can do this?

有帮助吗?

解决方案

Your answer looks fine to me, but if you don't want anything to happen after executing, you don't have to have the JsCmd do anything. For example:

SHtml.ajaxText(myVar, (str) => {
  myVar = str
  myMethod()
  JsCmds.Noop
})

Where the JsCmds.Noop, is pretty much the same as issuing return in plain JavaScript, so the function returns without doing anything. I put the codeblock inline for brevity, but you could also just do what you did with (str) => f(str) and have your method return JsCmds.Noop.

其他提示

I found a way, but is not so clean.

since the function (str) => myVar = str is also executed on submit, that's what I've done:

def f(str:String):JsCmd = {
  myVar = str
  myMethod()
  SetHtml("msg_div",<span> method executed </span>)
}

SHtml.ajaxText(myVar, (str) => f(str))

This way every time I submit a modification in ajaxText myMethod is executed but I'm forced to return some JsCmd from the f function.

If you find a better way to do that let me know please.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top