问题
I want to render d3 charts on the server. I had 3 options: Node, Phantom, and Nashorn.
I prefer Nashorn because my API is Scala Play and I don't want to manage another process. (deployment, load, queue, etc etc)
So now I need to get JSDom working in Nashorn, so that D3 will have something to render to.
This works so far but I can't figure out how to add 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)
}
}
回答1:
I'm loading a fork of domino in nashorn to run d3. Domino is a server-side DOM implementation based on Mozilla's dom.js. I loaded it using r.js from require.js. You can use this readfully function that is missing from nashorn to get require.js working.
来源:https://stackoverflow.com/questions/37425346/running-jsdom-in-nashorn