在我的ScalaJS项目中,我使用Semantic-UI和scala-js-jquery
我用它来修补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"))
。
我现在的问题是如何翻译这个:
// custom form validation rule
$.fn.form.settings.rules.adminLevel = function(value, adminLevel) {
return (window.user.adminLevel >= adminLevel)
};
答案 0 :(得分:1)
您的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
}
如果您有一些代表这些结构的东西,您可以使用静态类型做更好的事情,但如果您对动态类型解决方案感到满意,那基本上就是这样。