我想在服务器上渲染 d3 图表。我有 3 个选择:Node、Phantom 和 Nashorn。
我更喜欢 Nashorn,因为我的 API 是 Scala Play,而且我不想管理其他进程。 (部署、加载、队列等)
所以现在我需要让 JSDom 在 Nashorn 中工作,以便 D3 可以渲染一些内容。
到目前为止,这有效,但我不知道如何添加 jsdom
class Application @Inject() (val messagesApi: MessagesApi) extends api.ApiController {
def test = ApiAction { implicit request =>
ok("The API is ready")
}
def pptx = Action { implicit request =>
val manager: ScriptEngineManager = new ScriptEngineManager
val engine: ScriptEngine = manager.getEngineByName("nashorn")
engine.eval(new FileReader(Play.getFile("/ext/lodash.js")))
val output = engine.eval("function hello(){return _.join('Hello world nashorn does this thing'.split(' '), '-');} hello();")
Ok(output.toString)
}
}
最佳答案
我正在加载 domino 的 fork 在 nashorn 中运行 d3。 Domino 是基于 Mozilla dom.js 的服务器端 DOM 实现。我使用 require.js 中的 r.js 加载它。您可以使用this readfully nashorn 中缺少让 require.js 工作的函数。
关于javascript - 在 Nashorn 中运行 jsdom,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37425346/