【问题标题】:How to translate a JQuery function value in Javascript to ScalaJS (scalajs-jquery)如何将 Javascript 中的 JQuery 函数值转换为 ScalaJS (scalajs-jquery)
【发布时间】:2018-09-13 22:39:43
【问题描述】:

在我的 ScalaJS 项目中,我使用 Semantic-UIscala-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)
};

【问题讨论】:

    标签: jquery semantic-ui scala.js


    【解决方案1】:

    你的 JS sn-p

    // 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
    }
    

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

    【讨论】:

      猜你喜欢
      • 2018-09-12
      • 1970-01-01
      • 1970-01-01
      • 2017-02-04
      • 2014-05-26
      • 2018-07-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-20
      相关资源
      最近更新 更多