JavaScript dependencies through web-jars

One of the challenges of developing web applications is that we are writing two quasi-separate programs: the server-side program and the client-side program. These generally require different technologies. In particular, for any but the most trivial application, we must keep track of JavaScript libraries, and integrate processing the JavaScript code (for instance, for minification) in the build process.

The Play framework manages JavaScript dependencies through web-jars. These are just JavaScript libraries packaged as jars. They are deployed on Maven Central, which means that we can just add them as dependencies to our build.sbt file. For this application, we will need the following JavaScript libraries:

  • Require.js, a library for writing modular JavaScript
  • JQuery
  • Bootstrap
  • Underscore.js, a library that adds many functional constructs and client-side templating.
  • D3, the graph plotting library
  • NVD3, a graph library built on top of D3

If you are planning on coding up the examples provided in this chapter, the easiest will be for you to start from the code for the previous chapter (You can download the code for Chapter 13, Web APIs with Play, from GitHub: https://github.com/pbugnion/s4ds/tree/master/chap13). We will assume this as a starting point here onwards.

Let's include the dependencies on the web-jars in the build.sbt file:

libraryDependencies ++= Seq(
  "org.webjars" % "requirejs" % "2.1.22",
  "org.webjars" % "jquery" % "2.1.4",
  "org.webjars" % "underscorejs" % "1.8.3",
  "org.webjars" % "nvd3" % "1.8.1",
  "org.webjars" % "d3js" % "3.5.6",
  "org.webjars" % "bootstrap" % "3.3.6"
)

Fetch the modules by running activator update. Once you have done this, you will notice the JavaScript libraries in target/web/public/main/lib.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.182.73