如何将Javascript中的JQuery函数值转换为ScalaJS(scalajs-jquery)

在我的ScalaJS项目中,我使用了Scala-js-jquery的 Semantic-UI

我用这个来修补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) }; 

你的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 } 

如果你有一些代表那些结构,你可以用静态类型做更好的事情,但如果你对动态类型的解决方案感到满意,那基本上就是这样。