Monday, June 11, 2012

Javascript Web Applications and Spine.js

It has been a while since I even mentioned any particular book on this blog, but lately I have been reading a lot of technical books on my daily transit to and from work and this one stood out as one I received a lot of value from.

JavaScript Web Applications (Amazon link) by Alex MacCaw is about applying the discipline of the Model View Controller software design pattern to Javascript applications.

You would be forgiven for assuming these days that the only JavaScript knowledge worth knowing is how to use JQuery. Certainly only a few other domains within JavaScript get even a fraction of the ink devoted to that DOM selection and manipulation framework. My experience is that the topic of application design patterns is under served. As powerful as a framework like JQuery is it only really gives you more rope to hang yourself with if you don't have an actual application architecture.

Javascript Web Applications begins with explaining the basics of the Model View Controller pattern and why it (or something like it) is just as critical in modern JavaScript web applications as it is in the server back end. Alex MacCaw goes on to explain and demonstrate methods of implementing inheritance, models, views, controllers and custom events within Javascript. Along the way the author builds a library of objects and methods that he refers to throughout the book, lifting your frame of reference above the nuts and bolts.

Towards the end of the book I became aware that Alex MacCaw had actually created and continues to host a Javascript Framework called Spine.js (as opposed to backbone.js, a popular framework with a similiar mission) based on his work in this book. It is still in development and is now available in CoffeeScript. Continuing to look around at Alex's work I realise he has really impressive resume and for all that he has achieved (getting this book written amongst many other things) is ridiculously young.

Javascript Web Applications is a relatively opinionated book. This is something I don't mind. Alex MacCaw dislikes synchronous user interfaces and prefers his inheritance to be prototypical, not classical. His prescription for building JavaScript web applications is to separate the application into separate smaller, loosely coupled applications, sometimes organised into 'Controller stacks'. The result in the case of this book and framework is clear guidance on one approach to putting together an application.

Spine.js itself is a useful framework that is still very similar to the one he presents in his book. The ajax extension library makes persisting to a backend REST service very easy. An alternative extension does the same thing for HTML 5 local storage. The Spine.js website has a lot of useful documentation and patterns (with code in both CoffeeScript and JavaScript) and presents more server-side integration points (such as Node and Rails).

My first experiment with Spine.js was also my first using JQuery Mobile and REST in Grails. Learning these three things together was perhaps a mistake. I will write more about that application in a later post. In any case my interest in the JavaScript-centric programming and development has been rekindled in no small part to this book.

Having looked around the internet as I read the book, I was surprised at how many MVC JavaScript frameworks I found. I like spine.js and I am pretty comfortable with it now but many of these other libraries offer similar features and are worth investigating. I found a useful comparison of the different MCV frameworks for Javascript which served as a nice jumping off point for individual libraries (even if I don't agree with the author's conclusions)

No comments:

Post a Comment