Backbone.js Patterns and Best Practices book review

My thoughts…

I was really happy when I was given the opportunity to read and review this book. Although I have had a lot of experience in recent years using JavaScript, Backbone.js is relatively new to me and I have been trying to incorporate it as much as possible within my projects ever since I started learning about it. This hasn’t always gone as well as I had hoped and sometimes I’ve found myself in situations where the application architecture has become convoluted, making it harder to maintain.

So, this was the perfect book for me. The author is mostly focused on making your application architecture scalable, clean, and shares knowledge of the problems we’re likely to face and how to solve them.

The author mentions that the book is best suited for intermediate to advanced level Backbone.js developers but I felt that this would also suit a beginner. The basics the book covers are very easy to digest (eg.. when explaining Views, Models, Collections and Routes).

The code samples are simple to follow and with good explanation. I found the information in each chapter to be interesting, insightful and easy to understand. So all in all, very well written in my opinion.

Now that I have read this book, I  feel as though I can structure my projects in a more scalable way. It’s a valuable resource to me and I would recommend it to anyone who is thinking of using Backbone.js or already using Backbone.js but need professional guidance.

I have written a piece on each chapter and expressed parts that I have found most interesting.

3576OS_Backbone Practices

Purchase this book

Chapter 1 : Reducing Boilerplate with Plugin Development

The first chapter introduces Backbone.js and Underscore.js. It initially includes some basic examples of use including extending classes to reuse methods which most readers may already be familiar with but it’s useful for beginners.

What I was particularly interested in is the authors approach to the multiple class inheritance scaling problem in JavaScript. The author goes on to write about using JavaScript mixins efficiently to counter this problem.  The author ends the chapter well by explaining how to use currying to merge another functions arguments into another function. One great use case scenario for using currying is to pass another functions config into another.

Essentially this chapter gets the developer thinking about building the fundamentals to help scale a potentially complex Backbone.js project.

Chapter 2 : Working with Views

The author writes about the basic use of a Backbone.js view and explains the $el property which Backbone.js uses to inject view changes to the DOM. I was interested in learning about the pitfalls in using these view methods within multiple view instances. The author provides an experienced solution.

Another important part of using views is listening for changes in the view. Backbone.js offers a comprehensive solution to event listeners. If you are interested in how Backbone.js handles this in particular then the author explains this in detail.

When working with views, you need to connect them to your models. Your view listens for changes made by the client and passes it back to the model; the model then passes back to the view and renders it to the client.  The author explains how to do this efficiently in Backbone.js and making use of underscore.js templates to do so. It is very easy for me to understand how this works and I found it really interesting.

One of the more surprising additions to this chapter is the way in which we can partially update a view. It is not something I usually have to think about in my workflow at present but it adds another way of reusing code. If you use common events across multiple views then it would make sense to make a singular, more modifiable view.

The author writes about sub views which is pretty typical of Backbone.js applications but adds a solution to the multiple DOM reflow problem. It is also worth mentioning that the author talks about preventing memory leaks caused by sub views when destroying parent views.

Working with underscore.js templates
I am really glad the author went into great detail when talking about underscore.js templates. Very often when reading books or online resources, developers seem to prefer an alternative method such as ‘moustache’ or ‘handlebars’. I am personally using underscore.js templates more and more and I am delighted that the author has explained comprehensive scope and also provided a solution to storing these templates in JavaScript files in a human readable format. rather than the bloat of inline HTML.

Marionette
I hadn’t heard of Marionette until I read this book so in case you haven’t either, here is an overview based on my understanding:

Marionette provides a collection of common patterns and solutions to Backbone.js problems. It is a collection of packages that you can require so that you don’t need to include the complete library. In the book, the author  explains how to extend Backbone.js classes to use Marionette.  I found this interesting because Marionette adds a certain number of new custom events that are useful. This is definitely worth a read.

This chapter also includes a comprehensive explanation of how to use layoutmanager which is a template manager for Backbone.js.

Chapter 3 : Working with Models

The author begins this chapter by touching base with  models and describes some example form validation techniques and provides a solution for validating larger forms. I found the content easy to digest and understand and it provided me with some useful information about how to serialize server response data using the parse method.

The author ends this chapter by giving a comprehensive code example of using the Backbone-relational plugin to handle complex data structures which I found valuable.

Chapter 4 : Working with Collections

This chapter contains goes into detail about using AJAX to fetch from and save to the server using collections, and also sorting and filtering methods stored in collections.

There is a very interesting part towards the end of the chapter that explains how you can merge multiple model types into collections. This practice was of particular interest to me.

Chapter 5 : Routing Best Practices and Subrouting

Again, the author begins the chapter with the basics. Explaining the difference between hash fragments or the history API and  how to map methods to routes.

Routes are particularly important in any single page app since they are used to trigger different instances as the client interacts. For large scale apps with lots of routing, the author provides a solution to help split up the routing class by explaining how subroutes work which helps to modularise the class.

Chapter 6 : Working with Events, Sync, and Storage

The author writes about how to use events and a way to prevent memory leaks after removing DOM elements.

I found the explanation about using sync and storage very useful having not actually used it before. Backbone.js provides a very easy way to synchronise data from 3rd party API’s where the node names are different and require mapping to your own apps methods and models. This kind of thing has always been a long-term headache for me and reading this may very well have solved all of my problems here.

If you’ve not used HTML5 localStorage yet I would recommend reading the part in this chapter about offline storage (Offline storage using the Backbone.LocalStorage adapter).
This solves a fundamental problem in data synchronisation. To identify one example: if a user  loses an internet connection and is now offline, but is still modifying data, the data can be stored in the browser and synchronised when the user is back online. The author gives a comprehensive example of how to use HTML5 localStorage in Backbone.js apps which I found really interesting.

Chapter 7 : Organising Backbone Application – Structure, Optimise, and Deploy

The author recommends a directory structure which should be familiar to experienced developers but goes into detail about why it is laid out in this way. I, personally, have been adopting the modular approach to directory structure so I was happy to read that what I am doing is in-fact one of the more scalable approaches.

I was also interested to read about AMD (Asynchronous Module Definition) since this is becoming particularly common in modular management within apps now. I have been using require.js in every one of my projects since summer 2013 when I first heard about it and now cannot imagine life without it. If you’re not using this already – you’d better start.
The author explains how to configure require.js and explains it’s API in some detail which is useful if you’re not familiar with it.

In this chapter, the author also gives useful information about things to think about when planning the underlying architecture of your app, and describes some architectural patterns that may be quite useful to bear in mind.

Chapter 8 : Unit Test, Stub, Spy, and Mock Your App

If you are unfamiliar with the way you can unit test your app in JavaScript then this chapter is useful. I am glad the author included this. The book explains how you can set up test cases using QUnit and uses it to run a simple Backbone.js component test.

If you are interested in more in depth testing using spies, stubs and mocks then a section about that is included here. All this is useful if you want to evaluate expectations of results from your JavaScript code.

 

About 

I am a Web Developer from Salisbury, UK. In recent years I have been focused on WordPress and front end UI/UX development, constructing ways in which users can easily browse websites and providing admins with comprehensive and easy to understand management tools.

One thought on “Backbone.js Patterns and Best Practices book review”

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>