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.
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.
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 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