Since I had issues finding a good explanation on how to tie together a controller and a directive with isolated scope I decided to create my own blog post on this subject. This repo contains a runnable example of the solution. It contains a Spring Boot Web Application that can be started to act as a HTTP server but all the interesting stuff is in the src/main/webapp folder.
To create modular code with AngularJS you want to create reusable components; directives. Directives should not depend in any way on the parent controller. They should not be able to see any of the parent scope unless it's explicitly provided to them. To do this Angular directives can have an isolated scope (which in my opinion should be the default). This however leads to an issue: typically a directive needs information provided for them, needs to provide methods that can be called and often also has to fire events that the layers above the directive need to be able to respond to. Especially the latter part, informing the scopes above of changes, is done in a somewhat particular way.
Continue reading →
On a server you may want to deploy your application as a war. How to build a war with spray-servlet Locally it's easiest to run without an application server. We include both the spray-servlet and spray-can dependencies:
name := "sprayApiExample"
version := "1.0"
scalaVersion := "2.11.6"
libraryDependencies ++= {
val akkaV = "2.3.9"
val sprayV = "1.3.3"
Seq(
"io.spray" %% "spray-can" % sprayV,
"io.spray" %% "spray-servlet" % sprayV,
"io.spray" %% "spray-routing" % sprayV,
"io.spray" %% "spray-json" % "1.3.1", //has not been updated yet
"com.typesafe.akka" %% "akka-actor" % akkaV
)
}
//This adds tomcat dependencies, you can also use jetty()
tomcat()
Continue reading →
In Groovy we can use the sort
and unique
methods to sort a collection or remove duplicates from a collection. These methods alter the collection on which they are invoked. This is a side effect we might want to avoid. Therefore the sort
and unique
methods where changed and we could pass a boolean
argument to indicate if the original collection should be changed or that we must have a new collection as the result of the methods, leaving the original collection untouched. Since Groovy 2.4 we have two new methods which by default return a new collection: toSorted
and toUnique
.
In the following sample we see the new methods in action:
Continue reading →
When we construct an typed Array out of an existing List, we use the method T[] toArray(T[] a)
. When an array with a lower size than the size of the List is passed as argument, this results in a new array being created. Take a look at the implementation of ArrayList here. Using the method with an incorrect sized array is inefficient. Using the toArray
method directly with a correctly sized array is therefore preferable.
ArrayList myList; //Assume myList has some added entries
//Size is too small a 2nd array will be created
MyClass[] arr = myList.toArray(new MyClass[0]);
Continue reading →
We will use spray-servlet to build a war file of our API. So we can run it in a java app server. I assume we already have a working REST API. We will need a web.xml, under src/main/webapp/WEB-INF/:
spray.servlet.Initializer
SprayConnectorServlet
spray.servlet.Servlet30ConnectorServlet
true
SprayConnectorServlet
/*
Continue reading →
Since Groovy 2.4.0 we can get the indices from the elements in a collection with the indices
method. In addition to this method we can also use the withIndex
to combine an Iterable
with the indices directly. The output is a List
of tuples where the first item is the value of the Iterable
and the second the index value. We can pass an optional argument to the withIndex
which is the starting point for the index values. Another alternative is the indexed
method. The indexed
method returns a Map
, where the key of the entry is the index value and the entry value is the Iterable
value.
In the following example we use the withIndex
method. The sample of the alphabet is the same as in the blog post about indices, but rewritten with the withIndex
method:
Continue reading →
Groovy already has so many extra methods for working with collections. If we have to need to swap two elements in a collection we can use the swap
method. We provide the two index values of the elements we want to swap and Groovy swaps the elements.
In the following sample we have a simple list and swap all elements by invoking the swap
method two times:
Continue reading →
I would like to show different ways of using Spring's @Autowired
annotation: Constructor, Method and Field autowiring.
The examples I show are all a form of byType
autowiring mode (constructor
autowiring mode is Analogous to byType
). Take a look at the Spring Reference guide for more information on the Autowiring modes.
Create a constructor with a dependent bean as constructor parameter and add the @Autowired
annotation to the constructor. A big advantage of autowiring by constructor is that the field can be made final, and therefore may not be changed after construction.
Continue reading →
In Java 8 we can create a constructor reference. We must use the syntax Class::new
and we get a constructor reference. This syntax is not supported in Groovy, but we can use the method pointer or reference syntax .&
to turn a method into a closure. We can even turn a constructor into a closure and use it everywhere where closures are allowed.
In the following sample code we have a User
class with some properties. Via the User.metaClass
we can get a reference to the method invokeConstructor
and turn it into a method closure:
Continue reading →
Suppose I want to make coffee. This involves 4 steps:
- 1a. grind coffee beans
- 1b. heat water
- combine
- filter
Continue reading →