从scala.js调用jquery .each方法

时间:2015-11-02 17:16:56

标签: javascript scala scala.js

这个doc是否过时了? http://www.scala-js.org/doc/calling-javascript.html 似乎无法在intellij中获得这个权利:

import org.scalajs.dom.html
import org.scalajs.dom
import org.scalajs.dom.raw.HTMLElement
import scala.scalajs.js
import js.JSConverters._

import fr.iscpif.scaladget.d3._
import fr.iscpif.scaladget.mapping._

import scala.scalajs.js
import scala.scalajs.js.annotation.JSExport
import org.scalajs.jquery.jQuery

/**
 * Created by IDEA on 31/10/15.
 */
@JSExport
object LearnD3 {
  @JSExport
  def main(div: html.Div): Unit = {
    jQuery("#list > li").each((x, y) => y)
  }
}

第一个dom.HTMLElement似乎不存在。有org.scalajs.dom.raw.HTMLElement,但即使我导入了类型不匹配:

enter image description here

2 个答案:

答案 0 :(得分:5)

您需要指定参数的类型。以下代码有效:

FATAL EXCEPTION: main
11-03 18:56:17.703 32577-32577/com.votingsystem.tsiro.votingsystem E/AndroidRuntime: Process: com.votingsystem.tsiro.votingsystem, PID: 32577
11-03 18:56:17.703 32577-32577/com.votingsystem.tsiro.votingsystem E/AndroidRuntime: android.view.WindowManager$BadTokenException: Unable to add window -- token android.view.ViewRootImpl$W@421769f8 is not valid; is your activity running?
11-03 18:56:17.703 32577-32577/com.votingsystem.tsiro.votingsystem E/AndroidRuntime:     at android.view.ViewRootImpl.setView(ViewRootImpl.java:532)
11-03 18:56:17.703 32577-32577/com.votingsystem.tsiro.votingsystem E/AndroidRuntime:     at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:259)
11-03 18:56:17.703 32577-32577/com.votingsystem.tsiro.votingsystem E/AndroidRuntime:     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
11-03 18:56:17.703 32577-32577/com.votingsystem.tsiro.votingsystem E/AndroidRuntime:     at android.widget.PopupWindow.invokePopup(PopupWindow.java:1019)
11-03 18:56:17.703 32577-32577/com.votingsystem.tsiro.votingsystem E/AndroidRuntime:     at android.widget.PopupWindow.showAsDropDown(PopupWindow.java:925)
11-03 18:56:17.703 32577-32577/com.votingsystem.tsiro.votingsystem E/AndroidRuntime:     at android.support.v7.internal.widget.AppCompatPopupWindow.showAsDropDown(AppCompatPopupWindow.java:78)
11-03 18:56:17.703 32577-32577/com.votingsystem.tsiro.votingsystem E/AndroidRuntime:     at android.support.v4.widget.PopupWindowCompatKitKat.showAsDropDown(PopupWindowCompatKitKat.java:30)
11-03 18:56:17.703 32577-32577/com.votingsystem.tsiro.votingsystem E/AndroidRuntime:     at android.support.v4.widget.PopupWindowCompat$KitKatPopupWindowImpl.showAsDropDown(PopupWindowCompat.java:92)
11-03 18:56:17.703 32577-32577/com.votingsystem.tsiro.votingsystem E/AndroidRuntime:     at android.support.v4.widget.PopupWindowCompat.showAsDropDown(PopupWindowCompat.java:171)
11-03 18:56:17.703 32577-32577/com.votingsystem.tsiro.votingsystem E/AndroidRuntime:     at android.support.v7.widget.ListPopupWindow.show(ListPopupWindow.java:654)
11-03 18:56:17.703 32577-32577/com.votingsystem.tsiro.votingsystem E/AndroidRuntime:     at android.support.v7.widget.AppCompatSpinner$DropdownPopup.show(AppCompatSpinner.java:766)
11-03 18:56:17.703 32577-32577/com.votingsystem.tsiro.votingsystem E/AndroidRuntime:     at android.support.v7.widget.AppCompatSpinner.performClick(AppCompatSpinner.java:424)
11-03 18:56:17.703 32577-32577/com.votingsystem.tsiro.votingsystem E/AndroidRuntime:     at android.view.View$PerformClick.run(View.java:18446)
11-03 18:56:17.703 32577-32577/com.votingsystem.tsiro.votingsystem E/AndroidRuntime:     at android.os.Handler.handleCallback(Handler.java:733)
11-03 18:56:17.703 32577-32577/com.votingsystem.tsiro.votingsystem E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:95)
11-03 18:56:17.703 32577-32577/com.votingsystem.tsiro.votingsystem E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:136)
11-03 18:56:17.703 32577-32577/com.votingsystem.tsiro.votingsystem E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5146)
11-03 18:56:17.703 32577-32577/com.votingsystem.tsiro.votingsystem E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method)
11-03 18:56:17.703 32577-32577/com.votingsystem.tsiro.votingsystem E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:515)
11-03 18:56:17.703 32577-32577/com.votingsystem.tsiro.votingsystem E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
11-03 18:56:17.703 32577-32577/com.votingsystem.tsiro.votingsystem E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
11-03 18:56:17.703 32577-32577/com.votingsystem.tsiro.votingsystem E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method)

您可能会注意到索引val x = jQuery("#list > li").each((i: js.Any, y: Element) => y) 的类型为i,通常您希望索引的类型为Any。由于这种问题,jquery-facade诞生了:jquery的更强类型的外观。我强烈推荐它。 jquery-facade中的相同代码:

Int

或者,如果您不需要 import org.querki.jquery._ ... val x = $("#list > li").each((y: Element, i: Int) => y) 索引:

i

或者您希望 val x = $("#list > li").each((y: Element) => y) 类型被感染:

y

答案 1 :(得分:2)

实际上,这始终只是一个如何使用ThisFunction的概念性示例 - 对于松散类型的scala-js-jquery facade来说,它实际上从来都不是正确的。 (它更接近the jquery-facade version,虽然目前仍然需要Element而不是HTMLElement,因为这是JQuery文档所说的。我应该考虑是否应该收紧;我真的不确定。)

但基本上,从来没有打算成为文字代码,我相信它从来没有这样做过。它只是ThisFunction的语法说明。

至于HTMLElement,是的,那现在是raw。您现在通常将其称为dom.html.Element。在这方面,它 已过时。但无论如何,这个例子应该更多地作为一个概念而不是文字代码。 (是的,这个例子可能会使用重写; PR通常很乐意接受。)