JS: Closure

A closure is an inner function that has access to the outer (enclosing) function’s variables—scope chain.

The closure has three scope chains: it has access to its own scope (variables defined between its curly brackets), it has access to the outer function’s variables, and it has access to the global variables.

The inner function has access not only to the outer function’s variables, but also to the outer function’s parameters

You create a closure by adding a function inside another function.
A Basic Example of Closures in JavaScript:

function showName (firstName, lastName) {
var nameIntro = “Your name is “;
// this inner function has access to the outer function’s variables, including the parameter
function makeFullName () {
return nameIntro + firstName + ” ” + lastName;
return makeFullName ();
showName (“Michael”, “Jackson”); // Your name is Michael Jackson

Closures are used extensively in Node.js; they are workhorses in Node.js’ asynchronous, non-blocking architecture. Closures are also frequently used in jQuery and just about every piece of JavaScript code you read.

function celebrityID () {
var celebrityID = 999;
// We are returning an object with some inner functions
// All the inner functions have access to the outer function’s variables
return {
getID: function () {
// This inner function will return the UPDATED celebrityID variable
// It will return the current value of celebrityID, even after the changeTheID function changes it
return celebrityID;
setID: function (theNewID) {
// This inner function will change the outer function’s variable anytime
celebrityID = theNewID;

var mjID = celebrityID (); // At this juncture, the celebrityID outer function has returned.
mjID.getID(); // 999
mjID.setID(567); // Changes the outer function’s variable
mjID.getID(); // 567: It returns the updated celebrityId variable

To outcan can use console.log(mjID.getID()); then press F12 in the Chrome to see the console

Resetting and repopulating the elastic search on local

  1. vagrant up – this will start vagrant vm
  2. vagrant ssh – this will connect to the image
  3. sudo salt-call state.sls elasticsearch.reset_es_data – this will reset all data in elastic search
  4. go to http://local-ccpm.sls.ufinity.com/elasticsearch/reindex – and then click all the link. this will repopulate all the elastic search data on the local image.

The reason this might be needed is that you can get I/O exception while trying to test the assign activity

Interaction between View and Controller aka Flow

When sending a request to your application the following happens:

Note: some steps may not be necessarily required

  • The request arrives at your server (e.g. Tomcat). Depending on the context path in the url the server decides to which application the request belongs.
  • Depending on the url and the servlet mapping in the web.xml file of your application the server knows which servlet should handle the request.
  • The request is passed to the servlet filter chain which can modify or reject requests
  • The servlet takes control over the request. In case of your Spring application the spring Dispatcherservlet receives the request. Now Spring kicks in
  • The request is processed by mvc intercepters preHandle methods
  • The request is mapped to a controller based on the url. The corresponding controller method will be called.
  • Your controller is processing the request. Many different responses can be returned in controllers (jsp, pdf, json, redirects, etc.). For now i assume you want to render a simple jsp view. Result of the controller are two things: a model and a view. The model is a map that contains the data you want to access later in your view. The view at this stage is most of the time a simple string containing a view name.
  • Registered springs mvc interceptors can kick in again using the postHandle method (e.g. for modifying the model)
  • The ‘view’ result of your controller is resolved to a real View using a ViewResolver. Depending on the ViewResolver the result can be jsp page, a tiles view, a thymeleaf template or many other ‘Views’. In your case the ViewResolver resolves a view name (e.g. ‘myPage’) to a jsp file (e.g. /WEB-INF/jsp/myPage.jsp)
  • The view is rendered using the model data returned by your controller
  • The response with the rendered view will be passed to mvc interceptors again (afterCompletion method)
  • The response leaves the dispatcher servlet. Here ends spring land.
  • The response passes servlet filters again
  • The response is send back to client