如何将Javascript中的JQuery函数值转换为ScalaJS(scalajs-jquery) [英] How to translate a JQuery function value in Javascript to ScalaJS (scalajs-jquery)
本文介绍了如何将Javascript中的JQuery函数值转换为ScalaJS(scalajs-jquery)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的ScalaJS项目中,我将语义UI 与
In my ScalaJS project I use Semantic-UI with scala-js-jquery
我用它来修补JQuery:
I use this to monkey patch JQuery:
// Monkey patching JQuery
@js.native
trait SemanticJQuery extends JQuery {
def dropdown(params: js.Any*): SemanticJQuery = js.native
def popup(params: js.Any*): SemanticJQuery = js.native
// and more
}
// Monkey patching JQuery with implicit conversion
implicit def jq2semantic(jq: JQuery): SemanticJQuery = jq.asInstanceOf[SemanticJQuery]
例如$('select.dropdown').dropdown();
翻译为jQuery(".ui.dropdown").dropdown(js.Dynamic.literal(on = "hover"))
.
我现在的问题是如何翻译:
My problem now is how to translate this:
// custom form validation rule
$.fn.form.settings.rules.adminLevel = function(value, adminLevel) {
return (window.user.adminLevel >= adminLevel)
};
推荐答案
您的JS代码段
// custom form validation rule
$.fn.form.settings.rules.adminLevel = function(value, adminLevel) {
return (window.user.adminLevel >= adminLevel)
};
直接翻译为
import scala.scalajs.js
import scala.scalajs.js.Dynamic.{global => g}
// custom form validation rule
g.$.fn.form.settings.rules.adminLevel = { (value: js.Dynamic, adminLevel: js.Dynamic) =>
g.window.user.adminLevel <= adminLevel
}
如果可以用静态类型来表示那些结构,可以对静态类型做得更好,但是如果您对动态类型的解决方案感到满意,那么基本上就是这样.
You could do something nicer with static types if you have some to represent those structures, but that's basically it if you're satisfied with a dynamically typed solution.
这篇关于如何将Javascript中的JQuery函数值转换为ScalaJS(scalajs-jquery)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文