Monday, January 9, 2012

Sorting collections with Backbone.js and jQuery Mobile

In a previous post, I provided a simple example of rendering an HTML list view using Backbone.js and jQuery Mobile. The code from that example ended up rendering a list like this:

Notice that the list is presented in the order that came from the JSON.

Also, when items are added to the list, they show up at the bottom. While easy, this isn't the best. In this post, I'll add sorting to the backbone collection and make a small change to the view so that as new items are added, the list is automatically sorted.

Backbone.js makes these changes very easy. In order for a Backbone collection to be sorted, you implement a comparator method on the collection. As models are added to the collection, Backbone will make sure the sorting is maintained based on the comparator implementation. From the Backbone documentation "Comparator functions take a model and return a numeric or string value by which the model should be ordered relative to others".

We will sort our list based on the date the exercise activity occurred. Since comparators must return a number or string for sorting, we will return the time in milliseconds since 1/1/70. Our new collection definition looks like this: If we refresh our browser with the updated collection implementation, notice that the list is now sorted by date.

If we wanted it sorted descending, we would simply return the negative value of the getTime() method.

If you tried to add a new activity, you'll notice that it shows up at the end of the list. This is due to the fact that we bind a listener to the add method of the collection (line 7 below), and all this does is append the HTML to the end of the list view (line 30 below).

If you were to open a JS console in the browser and investigate the collection by entering

JSON.stringify(exercise.activities)
you would see that the collection is ordered. To fix the HTML representation of the collection all we need to do is replace the add event binding to call render. This will re-render the HTML based on the collection. We can also remove the add method in the View, as it is no longer needed. Our updated View configuration looks like this:

After refreshing the view, the list will continue to be sorted even when adding new activities. Since the code just adds an item with todays date, try typing the following in the JS console

exercise.activities.add({id:12, date:"12/01/2011", type:"Interval Run"});

This should add an item at the top of the list. Now your exercise activity will always be sorted.

The source code can be found in my git repository here. This URL will take you to the "sort" branch. The master branch represents the code from the introduction blog post found here

Related Posts:

Tuesday, January 3, 2012

Introduction to Backbone.js with jQuery Mobile

If you are working on a JavaScript heavy application (think jQuery Mobile, etc.), you probably will want to look at some JavaScript libraries to help add structure, consistency and convenience to your applications. One of the JavaScript libraries I've used lately is Backbone.js. To quote Backbone themselves, it provides "models with key-value binding and custom events, collections with a rich API of enumerable functions, views with declarative event handling, and connects it all to your existing application over a RESTful JSON interface.". This is really a great summary of what Backbone can provide to your application.

As an example of how to apply Backbone, I'll create a small jQuery Mobile app and use a Backbone model, collection and view to render the user interface. Through the use of Backbone's event model, the user interface will update as the model changes or new models are created.

I decided to use jQuery Mobile as the UI framework mainly because I am a mobile guy and it will provide the HTML with some reasonable style with no additional effort on my part. I know, lazy...but it works for this blog post :) Continuing on the lazy path, I will refer to jQuery Mobile as JQM from this point forward.

The following assumes you are at least a little familiar with jQuery and JQM. If not, take a look at the JQM website (http://jquerymobile.com/). They have some great documentation and even a template for creating a basic app. All of the code in this post will be available in my github repository (https://github.com/stevenpsmith/Exercise/tree/BackboneIntro).

The application we are going to make will allow a user to log exercise they have done on any given day. Please remember this is a very basic app for the sole purpose of demonstrating Backbone.js. The piece that will be developed for this post will involve pulling exercise data from a server and presenting it in a list view, a very common paradigm for mobile apps. In the end, our app will look like this:

The JSON we will be dealing with will look like this:

I like to organize my application structure a little differently than the JQM introduction shows, mainly for organizational reasons. My application structure looks like this:

All of our work will be done in index.html and app.js. In real applications I usually break down my JS files even further, but it really isn't necessary here. The list page of the application is defined within index.html. It should look like a typical JQM page definition with the only the content portion defined. We will use the Backbone View to create the list view:

If we view this in the browser, we get an empty page with an add button in the header. Since we want to populate our list view with data, we should define a Backbone model and collection in order to manipulate and store the data via JavaScript. A basic model and its collection are easily defined:

The great thing about the collection is it provides a RESTful access back to your server for data retrieval and persistence. To keep this simple, our data will be served from a static JSON file with the structure referenced earlier. Since we have no real server side in this example, we can't really save our changes anywhere but in the client's memory. But the collection will fetch the data from the server and can be used to help render our user interface. The following code will create an instance of the collection in memory, retrieve the data from the server, and parse it into a collection of models.

The exercise.initData() function can be called during initialization of the application (or wherever else might be appropriate) to load the data. Now it is time to render the view. Backbone views depend upon Underscore.js. Underscore is a utility library that includes a lot of great features, one of which is view templates. Backbone can be used with many view template frameworks (e.g. Mustache, Handlbars, etc.), but here we will stick with Underscore templates as they meet our needs. For our exercise activity items, we need a very basic template like this:

Giving the template a unique id will let us access it using typical jQuery selectors. The data driven parts of the template are surrounded by

<%= %>
As you can see in line 2, we are using the id, date, and type attributes of whatever model is supplied to the template to fill in the HTML accordingly. While the "identifier" attribute is not a real HTML attribute and not required by any of the frameworks, it would be used to determine what activity the user selected in the list so the appropriate data could be retrieved and displayed on a subsequent page.

The next step is to define the Backbone View that will render the list view. Backbone views can be defined and set up numerous ways, and the documentation does a great job explaining the options, etc. I like to define my views so that they are as self contained as possible. So, rather than allowing Backbone to create the HTML within a DIV (the default behavior), I define the appropriate properties to create a JQM list view as shown in lines 2-4 below:

From the Backbone site: "The default implementation of render is a no-op. Override this function with your code that renders the view template from model data, and updates this.el with the new HTML. A good convention is to return this at the end of render to enable chained calls". The $(this.el) refers to the containing element, in this case the ul element. There are standard attributes that are accessible from within the Backbone View including collection and model. Lines 17-20 iterate over the collection of activities, fills in the template placeholders with the data from the model, and appends the resulting HTML to the containing element.

If you need to pass arbitrary data to your view, it can be accessed via the options object as shown on line 11 of the code snippet. As mentioned earlier, I like my Backbone Views to be as self contained as possible, so I provide the containing HTML element for this view (as a jQuery object) when constructing the View instance. Line 20 sets the HTML of the containing element to the Backbone View and line 21 tells JQM to style things appropriately.

The next step is to instantiate and render the view with the appropriate collection and HTML container. Since this is the "home" page in our application, this is done after JQM initializes the page.

Now we have a list view that renders a JQM list based on data in a Backbone collection. To take advantage of the real power of Backbone models/collections, we can add listeners for changes to the collection that will update the view based on those changes. The beauty of this is the additional code to accomplish this is minimal. By adding

this.collection.bind('add', this.add, this);
to the initialize method of our Backbone View and providing an add() method.

Here we are just grabbing the list view and appending the new model to the view (and refreshing it so JQM does its magic). By adding a click handler to the "Add" button, we can test that adding a model to the collection updates the view.

There are other methods on which to bind, but this provides a quick example on how Backbone can be used to assist with view rendering and data handling in JavaScript based client applications.

The entire code base is available here. It can be deployed on any web server. For OS X, I use the built in web server, but feel free to use the server of your choice.

Related Posts:

Monday, December 19, 2011

Spring Roo 1.2.0 released - it's growing up...


On Saturday, 12/17/11, SpringSource/VMware released version 1.2.0 of Spring Roo, the Spring rapid application development platform, to the public.

There are a lot of good changes in this release to make Roo quite palatable to Enterprise developers, such as:
  • Multi-POM projects - now you can create web, service, database, and functional tiers and tie them all together with a parent POM.  You can even scaffold web interfaces based on models from other projects.
  • Scaffolded service support and repository support - now, instead of using the Active Record pattern (entities contain their own persistence methods) you can write your own services and repositories.  Or, let Spring scaffold them for you.  As usual, all scaffolded service and repositories will automatically contain the proper base methods, but you can then extend them and add your own, or push the code in and work with it yourself.
  • A cleaner add-on API - the Roo team has been working toward cleaning up the add-on internals and making it more consistent. Expect more add-on developers to appear after this release, it's really a straight-forward process to develop them.
  • JSF support - while this is the initial version of the add-on, it is rather feature-rich.  Roo supports either Oracle Mojarra or Apache MyFaces, and theming, and uses composed UIs.  In some ways, developing user interfaces with JSF may be easier for newer developers.  Unlike earlier efforts, though, Roo is not mounting JSF via the WebFlow integration.  Scaffolding is supported, as are rich widgets via PrimeFaces (www.primefaces.org).
There is more, including enhancements to GWT and some shell usability enhancements (you can now do a OS escape with the exclamation point and a space such as roo> ! ls).  We're waiting on an updated blog entry from Alan Stewart to detail everything.  For now, though, you can download Roo 1.2.0 from springsource.org/spring-roo.  Srini Penchikala and I have wrapped up writing chapters for Roo in Action, and are readying it for print in early April.  The book covers Roo 1.2.0, and will be helpful for those just starting out.  You can purchase the MEAP online at http://www.manning.com/rimple.

Ken is a writer, podcaster and trainer/mentor at Chariot Solutions. He teaches Spring, Maven, and other subjects, and you can see training courses offered by Chariot at chariotsolutions.com/education.

Friday, December 16, 2011

OpEd: Yammering about Scala, Java, and Winning

This is an opinion piece that represents the views of the author, and does not represent the official stance of Chariot Solutions. But sometimes it is fun to weigh in on the news of the day.

There has been quite a dust up in the blogosphere about Yammer's decision to switch from using Scala to Java. Google "yammer switching to scala" and you'll get a sampling. You can read it for yourself, but the gist of story seems to be that Yammer, the self described "enterprise social network" e.g. private label Twitter, decided to move their development from Scala to Java. The Scala guys at Typesafe heard about this (disclosure: Chariot Solutions is a Typesafe partner), and privately asked Coda Hale, infrastructure architect at Yammer, the reasoning behind the move. Coda responded in another private-but-soon-made-public email. It is a very long and detailed email, but in summary:
"The essence of it is that the friction and complexity that
comes with using Scala instead of Java isn't offset by enough productivity
benefit or reduction of maintenance burden for it to make sense as our default
language."
Then Hale clarified the purpose of his email and Yammer clarified their official position on Scala.  Yammer's decision has (re)ignited the debate over whether Scala is better or worse than Java.  If you read the comments section of this blog post from Stephen Colebourne, you get the general gist but here is a sampling that I can reprint and still keep this blog "family friendly":

In favor of Scala:
Some pretty serious masochists out there at Yammer. I wonder if their investors now about it.

Not in favor of Scala:
The code and build process was a rat's nest of pain and people should avoid using scala at all costs on any commercial project where these things can become a big issue.
I've used and learned lots of different languages but can't recall anything more painful than scala. Cross compiling and maintaining c codebases with native ui on different Linux/winblows envs was easier and less traumatizing than this. It's fine for academic pursuits and personal projects but you're smoking some serious crack to try and justify it commercially.

Opinion #1: Java is winning (like Tebow not Sheen)

The biggest thing that I take away from this whole incident is that Java is winning.  I don't mean Java is winning vs Scala or that Java is better than Scala.  I mean that Java is, in my opinion, an aging language with many quirks, with a fairly dysfunctional governing body in the JCP, and a yet-to-be-fully-trusted new caretaker in Oracle, full of the baggage of vendor compromises which nearly ruined JEE, and faces many very strong competitors on the JVM itself like Scala, Groovy, and Clojure, and yet -- against all odds and in very Tim Tebowesque form -- is winning over highly skilled engineering shops like Yammer.

I've been a Java programmer for 10+ years and there are many things about Java that irritate me like the slew of boilerplate code that you need in every single class.  But while innovation in the language itself can only go so far while still maintaining backward compatibility, innovation in the wider Java community is still thriving.  Java may not be the best language out there, but the battle tested JVM runtime combined with the countless mature open source tools and frameworks make the Java ecosystem as a whole arguably the richest one on the planet.

For a while I was convinced Java was the new Cobol: a language past its prime which due to its huge popularity will never die out completely, but not on the short list of candidate languages for a green field project.  Now I've changed my mind.  Java has its warts but there is value in maturity, and it still can compete head to head with any language ecosystem out there.

Opinion #2: Scala is winning too

Yes, Yammer moved from Java to Scala, but they make explicitly clear that they like Scala too.  It is just that Java make more sense for them and their specific business needs.  Yammer was able to build an impressive high volume system with a small engineering team using Scala.  One shop moving away from Scala does not constitute a trend.  Scala has a relatively young ecosystem but already some very impressive open source projects like Akka.  I expect the combination of Jonas Boner and Martin Odersky together in a single company to provide a real boost to Scala.

I've never used Scala on a real project but I'm in love with the syntax and what you can do with the language.  I know people who use it all the time and love it as well.  Scala is undeniably more complex than Java but with the complexity comes more power.  Scala isn't better or worse than Java.  It is just different.

Opinion #3: Polygot is hard

I really like the idea of polygot programming: pick the programming language that fits your problem space.  I worked with many large clients that solve every problem with the same Java stack even when it is obviously a bad fit.  But one of the things that Hale mentioned in his email was that the choice for Yammer wasn't between Scala and Java, it was between Scala/Java and Java.  The context switching caused by thinking in multiple languages was a drag on the team.  When you think about it, a programmer is required to know many languages just to build a basic web app using a Java stack: Java, SQL, HTML, Javascript, and CSS.  Adding more languages to the mix makes things that much more complex.  Then you also include build tools, language idioms, and the ecosystem of libraries and frameworks.

I work in both Java and Groovy, and I find those languages similar enough to minimize the difficulty of context switching, but I can imagine switching between diverse languages like Java and Clojure might make my head explode.

Opinion #4: Coda Hale is a humorous writer

Not everyone can write about techie stuff and make it humorous and informative.  Here is a quote from his blog post that made me chuckle:

"The Scala community needs another giant blog post about ways in which someone doesn’t like Scala like I need a hole in my head, and I’d rather suck a dog’s nose dry than lend a hand to the nerd slapfights on Hacker News."

I was pleasantly surprised to find that Hale is speaking at my company's annual technology conference Philly ETE.  That's one session I'll be attending.

And I might just ask him if Scala is better than Java :-)

Monday, November 7, 2011

Groovy Algorithms: Shunting Yard

Groovy's sugary syntax makes coding algorithms -- dare I say it -- fun.  The shunting yard algorithm, invented by Dutch computer scientist Edsger Dijkstra, is used to parse mathematical expressions.  You might have picked up on this already, but computers and people think differently.  We commonly use infix notation to write mathematical expressions.  It is what you learned in school.  For example: "3 * (2 + 3)".  Makes sense to you, but it is inefficient for a computer to process.  Things like operator precedent and nested parenthesis make infix rather messy.  A more efficient format is postfix or Reverse Polish notation.  In postfix, the operands are listed first, and then the operator.  For example: "2 3 + 3 *".  This eliminates the need for parenthesis, and allows you to calculate the expression using just a single stack to hold operands and intermediate results.  Why didn't we learn this in school?  I don't know.  Why aren't we using the metric system yet?  Why doesn't Siri on my iPhone understand anything I say even though it works great in the commericals?  Life's little mysteries.  Back to shunting yard.  Here is my Groovy implementation of it.

The main class is ExpressionSolver.  The computePostfix method (line 7) shows how easy it is to calculate a postfix expression with a single stack and single pass through the tokens in the expression.  It delegates the work of converting from infix to postfix to the appropriately named ShuntingYardAlgorithm class (line 21).

1:  package org.jadcon.expression  
2:  def infixExpression = '5.5 + ((6 + 2) * 4) - -3 * 1.5'  
3:  def postfixExpression = '5.5 6 2 + 4 * -3 1.5 * - +'  
4:  def expectedResult = 42  
5:  assert computePostfix(postfixExpression) == expectedResult   
6:  assert computeInfix(infixExpression) == expectedResult  
7:  def computePostfix(postfixExpression) {  
8:       def stack = []  
9:       postfixExpression.split().each { token ->  
10:            if (token.isNumber()) {  
11:                 stack.add(token)  
12:            } else {  
13:                 def b = stack.pop().toBigDecimal()  
14:                 def a = stack.pop().toBigDecimal()  
15:                 stack.add(Operator.valueFrom(token).calculate(a, b))  
16:            }  
17:       }  
18:       stack.pop()  
19:  }  
20:  def computeInfix(infixExpression) {  
21:       computePostfix(ShuntingYardAlgorithm.convertToPostfix(infixExpression))  
22:  }  

The ShuntingYardAlgorithm class coverts an expression from infix to postfix.  It requires two stacks: an output stack and a working stack.  The main logic is in the processToken method (line 14) which loops through each token.  Operands go directly on the output stack.  Operators and parenthesis are placed on the working stack to get correctly ordered when placed on the output stack.

1:  package org.jadcon.expression  
2:  class ShuntingYardAlgorithm {  
3:       def static convertToPostfix(infixExpression) {  
4:            def output = []  
5:            def stack = []  
6:            evenSpaceAroundParens(infixExpression).split().each { token ->  
7:                 processToken(token, output, stack)  
8:            }  
9:            while (!stack.isEmpty()) {  
10:                 output.add stack.pop()  
11:            }  
12:            output.join(' ')  
13:       }  
14:       def static processToken(token, output, stack) {  
15:            if (token.isNumber()) {  
16:                 output.add token  
17:            } else {  
18:                 switch (token) {                           
19:                      case "(":  
20:                           stack.add token  
21:                           break  
22:                      case ")":  
23:                           while (stack.last() != "(") {  
24:                                output.add stack.pop()  
25:                           }  
26:                           stack.pop() // pop the left paren and ignore it  
27:                           break  
28:                      default:  
29:                           def operator = Operator.valueFrom(token)  
30:                           while (!stack.isEmpty() &&  
31:                                     stack.last().is(Operator) &&  
32:                                     operator <= stack.last()) {  
33:                                output.add stack.pop()  
34:                           }  
35:                           stack.add operator  
36:                 }  
37:            }  
38:       }  
39:       def static evenSpaceAroundParens(string) {  
40:            def output = []  
41:            string.each { letter ->  
42:                 if (!output.isEmpty() && letter != ' ') {  
43:                      def last = output.last()  
44:                      if (isParen(letter) && last != ' ') {  
45:                           output.add ' '  
46:                      } else if (isParen(last)) {   
47:                           output.add ' '  
48:                      }  
49:                 }  
50:                 output.add letter  
51:            }  
52:            output.join()  
53:       }  
54:       def static isParen(letter) {  
55:            letter == '(' || letter == ')'  
56:       }  
57:  }  

The final class is Operator which is an enumeration of operators.  Groovy makes this code pretty tight using closures.  Even threw in a spaceship comparison operator for good measure (line 24).

1:  package org.jadcon.expression  
2:  enum Operator {  
3:       PLUS('+', 0, { a, b -> a + b }),   
4:       MINUS('-', 0, { a, b -> a - b }),   
5:       MULTIPLY('*', 1, { a, b -> a * b }),   
6:       DIVIDE('/', 1, { a, b -> a / b })  
7:       String value  
8:       int precedence  
9:       Closure calculate  
10:       private Operator(String value, int precedence, Closure calculate) {  
11:            this.value = value  
12:            this.precedence = precedence  
13:            this.calculate = calculate  
14:       }  
15:       def static valueFrom(string) {  
16:            for (operator in Operator.values()) {  
17:                 if (operator.value == string) {  
18:                      return operator  
19:                 }  
20:            }  
21:            throw new IllegalArgumentException("invalid operator ${string}")  
22:       }  
23:       def int compareTo(other) {  
24:            precedence <=> other.precedence       
25:       }  
26:       def String toString() {  
27:            value  
28:       }  
29:  }  

Coding the shunting yard algorithm was fun exercise in Groovyness.

Wednesday, October 26, 2011

Spring Roo 1.2 will be tipping point for RAD Java

I'm going to make a bold statement: Spring Roo is about to do for Spring developers what Ruby on Rails did for Ruby developers - make developer agility, and not mechanical, rigid architectural structures, the focus of daily developer productivity.

There. I've said it. But I have to step back for a minute and disclaim a few things. First, as some of you may know I've been working with Spring and Spring-related projects for a while. I'm also the education director here at Chariot, and Spring has been a mainstay of many of our Java-based consulting projects and course materials - we're also VMware Training Partner. Finally, I'm an author on Spring Roo in Action by Manning, which is undergoing the transformation from draft to publication by early 2012.

With the disclaimers aside, let's dig into my treatise.  First, a bit about what, exactly, Roo is...

Roo in a nutshell

Roo is a developer productivity tool that uses a simple command-line shell to configure projects, install features, and create various objects such as JPA entities, controllers, services, repositories, and tests.  Conceptually, it is similar to Rails, Grails, or even JBoss Seam, in that you use it to bootstrap a project.

Unlike these other frameworks, which lean heavily on dynamically generated code, Roo is a compile-time framework - no "Roo Runtime" exists to interpret your code.  The shell adjusts your code and configuration, based on changes made to annotations in source code (either by the Roo shell, or by the developer).  Roo even generates setters, getters, JPA code, transaction logic, scaffolded controller views, and a lot more.

Monday, October 17, 2011

Akka Scheduler: Actors with Heartbeats

Akka is an excellent platform for writing concurrent applications using the Actor model. Chariot architect Anatoly Polinsky describes the Akka scheduler and how you can use it to create a heartbeat for an actor. From Anatoly's blog:

AKKA Scheduler: Sending Message to Actor’s Self on Start

Akka has a little scheduler written using actors. This can be convenient if you want to schedule some periodic task for maintenance or similar. It allows you to register a message that you want to be sent to a specific actor at a periodic interval.

How Does AKKA Schedule Things?

Behind the scenes, AKKA scheduler relies on “ScheduledExecutorService” from the “java.util.concurrent” package. Hence when AKKA Scheduler needs to schedule “a message sent to an actor, given a certain initial delay and interval”, it just wraps the task of sending a message in a “java.lang.Runnable”, and uses a “ScheduledExecutorService” to schedule it:
service.scheduleAtFixedRate( createSendRunnable( receiver, message, true ),
                           initialDelay,                         
                           delay,                             
                           timeUnit).asInstanceOf[ScheduledFuture[AnyRef]]

“Heartbeat” Actor

Let’s look at the example of scheduling a message that should be sent to an Actor’s “self” as the Actor on start. Why? Because it is a cool use case : )
“Heartbeat” would be an ideal example of such use case => “When a ‘Hearbeat Actor’ starts, it should start sending heartbeats with a given interval (e.g. every 2 seconds)”

Creating a Message

First we need to create a message that will be scheduled to be sent every so often. We’ll call it a “SendHeartbeat” message:
sealed trait HeartbeatMessage 
case object SendHeartbeat extends HeartbeatMessage

Read the full post at: AKKA Scheduler: Sending Message to Actor's Self on Start