I’ve been looking at getting a kickped for quite some time. I live about 1 1/2 miles away from the train and every day I drive my car and park in a parking garage. I pay $30 a month to park in the garage. I’ve been looking for an easier way to do the commute. I thought about riding my bike but I don’t really want to have to lock it up every day. All the bikes that people leave the train are usually beater bikes. In the morning I don’t have very much time and that is why scooters appeal to me. With a scooter I can just pull right up to the train and fold it up. It looks like a fun way to get to work. Also when I get to the city all I have to do is exit the train station, unfold the scooter, and I can get to work a lot faster than I would walking.
Single Page Web Application – part two
In my second post about single page web applications I want to talk about how to organize code. The first single page web applications we used Dojo. I was very attracted to the way they used modules. Each component in Dojo would be created in it’s own file called a module. The top of the module would have zero or more dojo.require statements which would indicate the current module’s dependencies. When loading the module dojo would execute these statements and load these dependencies and all of the transitive dependencies. You can visualize a dependency graph. The benefit of breaking up JavaScript into modules such as this is that it may, if done correctly, lead to much more organized cohesive code.
I thought this was great when I first learned about it. When most people think of JavaScript they recoil in horror at the thought of huge files containing poorly organized unrelated functions. If you view the source of many applications today you may still see that.
When I started working last year on my current project, I started researching modules. I came upon RequireJS. There are two main functions in RequireJS:
- require: Takes 2 arguments. The first argument is an array of dependencies where each dependency is a string name of a JavaScript file. The second argument is a call back function. The call back functions args should match the list of dependencies. RequireJS will pass the dependencies listed in the first parameter to the callback function in the second parameter. A file containing a require statement would be the main file loaded by your page and would typically bootstrap your application.
- define: Again takes two arguments just like require. An array of dependencies and a callback function. This has the same semantics as the require statement. Usually a require statement would reference one or modules created using define. Each JavaScript file should have one define statement. Define statements can contain dependencies on other modules containing their define statements with their own dependencies. Hence you end up with a web of dependencies.
To demonstrate, I’ve created a simple hello world application using RequireJS and Knockout here. The code is available on GitHub. This is just a very simple app. I am planning to write a more complicated app demonstrating knockout later.
<!DOCTYPE html> <html> <head> <meta content="text/html; charset=utf-8" http-equiv="Content-Type"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link href="css/bootstrap.css" rel="stylesheet" media="screen"/> <script type="text/javascript" data-main="js/app/main/main" src="js/lib/require-jquery.js"></script> </head> <body> <div class="container"> <div class="row"> <div class="offset3 span5"> <form class="form-horizontal"> <div class="control-group"> <label for="name" class="control-label">Name</label> <div class="controls"> <input type="text" id="inputName" placeholder="Enter your name to say hello" data-bind="value: name, valueUpdate: 'keyup'"> </div> </div> </form> </div> </div> <div class="row"> <div class="offset5 span5"> <h1>Hello <span data-bind="text: name"></span></h1> </div> </div> </div> </body> </html>
Line 7 above is where you include RequireJS. Notice the data-main property in the tag. This is where you reference your main application file that RequireJS will load. In this example, that file is main.js located in the js/app/main directory. That file is here:
requirejs.config({
baseUrl: "js"
});
require(["app/model/HelloWorldModel", "lib/knockout"],function(HelloWorldModel, ko){
var helloWorldModel = new HelloWorldModel();
ko.applyBindings(helloWorldModel);
});
The first statement on line 1 above is where the configuration of RequireJS is specified. It is just an object literal. In this simple example I am telling RequireJS to use the js directory as the base url. Without this statement RequireJS will try to resolve all urls relative to the directory of this script. On line 5 is the require statement. The first argument is the array of dependencies. Here it is a file called HelloWorldModel.js located in the js/app/model directory, and knockout.js located in the js/lib directory. The baseUrl property allows you to omit the js/ directory for each file, and you do not need to specify the .js file extension. The 2nd argument is a function callback that will be provided with the HelloWorldModel and knockout as the first and second arguments respectively. RequireJS will download these files and their dependencies and then provide these as arguments to the callback. The last statement binds the HelloWorldModel to the HTML page above.
The last file is HelloWorldModel:
define(["lib/knockout"],function(ko){
return function(){
var self = this;
self.name = ko.observable();
};
});
On line 1 is the define statement. Here the dependency list has only one entry which is knockout. Again RequireJS will locate this dependency and provide it to the 2nd argument which like the require statement is a function callback. Here we are defining a module that can be used as a dependency in other define or require statements. In this example I am defining a simple object that has one property called name. You notice that it is set to a value that is the result of a call to a function provided by Knockout called observable. This is the most basic functionality provided by Knockout. It is what allows for two way binding between your JavaScript model and your view.
The ko.applyBindings function in main.js bound the HelloWorldModel to the view shown in the first example. If you look at the view you notice there is a text box on line 17 as follows:
<input type="text" id="inputName" placeholder="Enter your name to say hello" data-bind="value: name, valueUpdate: 'keyup'">
The attribute data-bind=”value: name” bind this text box to the value in HelloWorldModel. Typing in the text box changes the value contained in the model automatically. To illustrate, the value of name is also bound to a span element. Typing in the text box automatically displays the value of the text box in the span just to the right of the word Hello:
<h1>Hello <span data-bind="text: name"></span></h1>
You can try the example here. I think it is a pretty simple example of how easy it is to bind values from your domain model to your view with very little code. I will dive more into Knockout in my next example
My first post can be found here.
Single Page Web Application – part one
In the past year I have done a lot of JavaScript development. My assignment was to create an application with a rich user interface that had the interactivity of a desktop application.
I had been part of the development of a single page web application once before about 3 years ago. That application utilized Dojo which is a very powerful front end JavaScript framework and toolkit. Dojo comes with many prebuilt widgets and has a powerful mechanism to extend existing widgets and create new ones. On that application I mainly wrote the services that the browser app required.
Most of the development I had done prior to that had been traditional web applications. I have a Java background so that meant I used Struts 2 and Spring MVC. This architecture relies on the user submitting a request to the server. The server then processes the request and generates the html to display back to the user. Many times with this type of architecture, much of the page does not change, but you force a complete re-rendering of the entire page. It is rather wasteful.
Single page web applications on the other hand only refresh a part of the page. In the example I mentioned above for the first single page app I wrote, we would make an AJAX request to a Struts 2 web application. It would call whatever necessary back end services we required, However instead of generating HTML, we would populate a view model and then Struts 2 would automatically serialize it to JSON. Thus the only thing going back and forth between the browser and the server was JSON. All rendering was done client side. You can also have the server render a snippet of HTML to update part of the page.
The benefit of this model of development is the page is much more responsive. The amount of information going across the wire is generally smaller.
Since the development of that app, there has been a lot going on with client side web development. Google has been a big leader demonstrating what you can do with JavaScript. Google Maps, Gmail and Google Docs are all awesome examples of what can be done with JavaScript.
The first thing that I did was do a lot of reading about some of the various frameworks and libraries that were out there. I was probably reading about 2-4 hours per day. I also dove back into JavaScript because it had been a while. You are not going to be able to create a large Single Page Application without a strong foundation in JavaScript. (I love learning computer languages. I had spent the last year playing around with Erlang).
Some resources I’ve used to learn.
- Javascript: The Definitive Guide - This would be the first JavaScript book I’d read. It is a large book, but you don’t need to read the whole thing. I’d focus on the core JavaScript part of the book. The back of the book is mainly an appendix. I’ve owned two editions of this book.
- Secrets of the Javascript Ninja - This book was written by John Resig, the creator of jQuery. He stresses the importance of the functional nature of JavaScript. People coming from languages like Java and C# really need to understand the importance of functions.
- Mozilla Developer Network - Great JavaScript resource.
- DailyJS - Really good JavaScript blog. Tells you what’s going on with new/existing libraries. I read this every day.
The main library I ultimately decided to use for my app is called Knockout. It is a data binding framework that allows two way binding between your JavaScript object model and your page. Their website has excellent tutorials on the basics of the framework and the various bindings that are available. If you click on the Tutorials link they have a few different lessons you can take.
My next post is here and talks about how to structure your code.
Using SpringSource Tool Suite
I’ve been a long time Java developer and have always used Eclipse. Eclipse on it’s own though doesn’t always come with all the plugins you want. Eclipse has gotten better over time with adding new plugins, but it used to be kind of painful to add new plugins as you’d have to also satisfy the plugin’s dependencies as well. I have always like to start off using SpringSource Tool Suite. It comes with plugins for Spring IDE, M2Eclipse for Maven projects, AspectJ tools, the web platform tools.
This post will detail getting started with Spring Source Tool Suite and creating an initial Maven project. I will detail all the steps necessary.
- If you haven’t already, get the JDK and not just a JRE. In Windows, set an environment variable JAVA_HOME to the directory where the JDK is installed. In my case I like to install a lot of things in an apps directory. Many tools do not like a space in the path, so you don’t want to put it in Program Files. In my case JAVA_HOME is set to C:\apps\Java\jdk1.7.0_10. Next add %JAVA_HOME%\bin to your path.
- Download and install Spring Source Tool Suite (SSTS) for your platform.
- The installer will prompt you to install Maven which you should. I also like to install Maven locally on my computer so I can run it from the command line. Follow the instructions for installing Maven outside of Eclipse.
- Once everything is installed, open SSTS. To get started with Maven, you’ll want to ensure you index the Maven Repositories. The repositories are where many of the libraries you’ll want to use are made available. For instance Apache has a repository. You can browse it here. Indexing the repository in Eclipse helps you to search for artifacts you will want to add to your project.
- You need to open the Maven Repositories view. To do so, go to the Window->Show View->Other menu.
- In the filter box type Maven. Maven Repositories will be shown in the list. Select it and press OK.
- Now you will see the Maven Repositories. There should be one entry called central under Global Repositories which you will see once you expand that view. Right click on central and select Enable Full Index. This will take a while. While updating, the entry will show in italics and the word updating will show within brackets.
- Once the repository is finished updating, you are ready to create your first project using Maven.
- Go to File->New->Other menu.
- In the filter text box, type Maven. Select Maven Project and click Next.
- Leave the defaults on the next screen and click Next.
- Now you will be on the screen where you pick your archetype. In Maven, an archetype is like a template for creating a project. In this example we will leave the default which is maven-archetype-quickstart. This archetype is for a simple Java project. I will use this in future posts that I am going to do about Spring Integration. From here click Next
- Now you will be asked to enter the GroupId, ArtifactId, Version and Package. The first three are the coordinates that another project would use if they wanted to reference your project. GroupId is similar to a Java Package. I would usually put com.yourorg. ArtifactId would be your project name. Version is used to create different versions for your application. If it is not an official release, you would typically have -SNAPSHOT appended to the end as it does when you create your first application. Package I would leave the default. After entering these, click Finish.
- Now you have your first Maven application. To test your configuration, you can build it in Eclipse. Right click your project and select Run As -> and the second Maven Build. This will bring up the Edit Configuration window. Under Goals, type package. Then click the Run button. In the Console window you will notice Maven downloading a bunch of packages. If you go to the target folder in Maven and refresh it, you should see a new Jar file for your project.
To learn more about Maven, look at their Getting Started Guide. My next post will start to look at Spring Integration.
Welcome to my blog
Welcome to my blog. This is my first post. I am a software architect at a Fortune 100 company. I intend to write about some of the software that I’ve worked with. I have been doing application development since June 1999.
Most recently I have been doing a lot of JavaScript development. I am currently the lead architect for a Single Page Application. We are using Knockout to handle two-way binding between the object model and the UI. Amplify is used to allow various components on the page communicate with each other using events. The page is loaded once and then all communication with the server is done via JSON. All rendering is done client side.
I’ve primarily done front end development. I read and experiment with lots of different technologies. For work assignments, I’ve used Struts, Struts 2 and Spring MVC as the MVC framework. Additionally I’ve been a heavy user of Spring for Inversion of Control. I have used Hibernate for ORM for two applications. I’ve written one application using Groovy and Grails. I also spent a lot of time playing around with Erlang which is like a programming language, middleware system, and toolkit for creating highly available, very reliable and bulletproof applications rolled into one.