Monday, February 17, 2014

Book Review: Backbone.js Patterns and Best Practices

The sub-title of Swarnendu De's Backbone.js Patterns and Best Practices (Packt Publishing, 2014) is "A one-stop guide to best practices and design patterns when building applications using Backbone.js." The Backbone.js site describes Backbone.js as follows:

Backbone.js gives structure to web applications by providing 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 API over a RESTful JSON interface.

The Preface states that most of the book's examples can be followed with simple text editor and web browser, but some examples require a web server and Node.js. The Preface points out that Backbone.js Patterns and Best Practices "is not a general introduction to Backbone.js or JavaScript design patterns," but is instead intended " for any developers who ha[ve] a basic knowledge of Backbone.js and is looking for solutions to common Backbone.js problems, looking to enforce reusability in their code by removing boilerplate and developing custom plugins and extensions, and hoping to use the most effective patterns to develop large-scale web application architecture."

The Preface further reinforces the point that this book is not for beginners when talking about the book's objective: "Whether you are an intermediate- or advanced-level Backbone.js developer, this book will guide you through the best practices and patterns to handle different issues with each Backbone component."

Chapter 1: Reducing Boilerplate with Plugin Development

The initial chapter of Backbone.js Patterns and Best Practices introduces the basics of Backbone.js and provides short descriptions of Backbone.Model, Backbone.View, Backbone.Collection, Backbone.Router, and Backbone.Events. The first chapter introduces the Underscore.js utility and mentions that "Backbone.js has a hard dependency on Underscore.js." The author also mentions Lo-Dash as an Underscore.js alternative. The core coverage of this initial chapter is on reusing code by extending (with and without a base class) and using mixins.

Chapter 2: Working with Views

Chapter 2 of Backbone.js Patterns and Best Practices begins with the sentence, "Backbone view works as the presentation layer of an application." The chapter is dedicated to coverage of Backbone Views and covers the el property, listening to view events, working with nested views, working with templates, organizing templates, precompiling templates, and more. Along the way, the chapter mentions the JavaScript Memory Profiling, Backbone-Debugger, Require.js, Handlebars.js, backbone.stickit, rivets.js, and Backbone.ModelBinder.

The second chapter also introduces Marionette.js, which it describes as "a composite application library for Backbone.js." The focus is on Marionette's ItemView, CollectionView, and CompositeView.

Chapter 2 moves finishes with coverage of backbone.layoutmanager as an alternative to Marionette RegionManager.

Chapter 3: Working with Models

Backbone.js Patterns and Best Practices's third chapter discusses working with Backbone models. The chapter begins by outlining the basics of Backbone models and how to use Backbone models with an emphasis on CRUD (create/read/update/delete) operations. Model validation is covered next with examples of using direct Backbone model validation as well as using Backbone.Validation. Chapter 2 covers serialization of Backbone models and using Backbone-relational.js for relational data work with Backbone.

Chapter 4: Working with Collections

Chapter 4 of Backbone.js Patterns and Best Practices defines a Backbone collection as "an ordered set of models" that includes "functionality to add, remove, sort, and filter models, and save to or fetch data from the server." The chapter includes a brief overview of Backbone collections before moving onto describing how to perform database-related operations on a Backbone collection. Sorting and filtering of Backbone collections are also covered in the fourth chapter.

Chapter 5: Routing Best Practices and Subrouting

The fifth chapter in Backbone.js Patterns and Best Practices describes a Backbone router as a mechanism that matches a "URL fragment" to an invoked method. The chapter provides an overview of Backbone routers and routing before moving onto coverage of "best practices for working with routers." In the course of this discussion, the author references Marionette.AppRouter and Marionette.Controller. Sub-routing is described in Chapter 5 and the extension backbone.subroute is specifically referenced.

Chapter 6: Working with Events, Sync, and Storage

Chapter 6 of Backbone.js Patterns and Best Practies covers "custom events, Backbone.sync() method and Backbone.LocalStorage." The portion of the chapter on events begins with a definition of events from the Backbone.Events API page. This section demonstrates how easy it is to create custom events in Backbone, contrasts use of Events to use of Callbacks, and provides a case study describing use of events. The section of Chapter 6 on Backbone.LocalStorage discusses and demonstrates use of Backbone.localStorage Adapter before mention an alternative called Backbone.dualStorage.

Chapter 7: Organizing Backbone Applications – Structure, Optimize, and Deploy

As its title suggests, Chapter 7 of Backbone.js Patterns and Best Practices discusses issues related to application structure, optimization, and deployment of Backbone-based applications. The author proposes an application directory structure which "is not something that will work universally for every application," but "lots of developers use it for their projects without any issues."

One of Chapter 7's main themes is Asynchronous Model Definition and using Require.js to add AMD support to a Backbone application.

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

The final chapter (not including appendices) of Backbone.js Patterns and Best Practices begins with the opinionated statement, "Majority of developers believe that testing is essential, but in reality only a few of them actually go for test-driven development." The author lists multiple tools/frameworks for testing JavaScript code, but the chapter's focus is on use of QUnit (unit testing framework) and SinonJS (test doubles support).

Appendix A: Books, Tutorials, and References

Appendix A includes the author's recommendations for three books on Backbone.js along with recommended online tutorials and blog posts on Backbone.js. This appendix also includes references to additional resources on testing Backbone-based applications with QUnit and SinonJS as well as references to more Backbone plugins and tutorials.

Appendix B: Precompiling Templates on the Server Side

Appendix B is short and uses a Node.js-based example to illustrate how to precompile JavaScript templates.

Appendix C: Organizing Templates with AMD and Require.js

Although two chapters earlier in Backbone.js Patterns and Best Practices discuss AMD and Require.js briefly, both chapters reference Appendix C for additional details. The example in this appendix is built on top of requirejs-tpl.

General Observations
  • My most important observation is that the implication of Backbone.js Patterns and Best Practices's title and the explicit statements in its Preface that the book is targeted at intermediate Backbone developers should not be taken lightly. Although the book does provide some brief introductory material on most of the subjects it covers, at least some familiarity with the basics of a Backbone.js-based application would be extremely beneficial in realizing the full value of reading this book.
  • There are not many images or screen snapshots in this book, but these are not particularly important to the content.
  • Even in the PDF version of Backbone.js Patterns and Best Practices that I reviewed, the code listings are black text on white background with no color syntax. Most of the code listings are short enough to mitigate the issue, but color-coded syntax would be easier to read.
  • Backbone.js Patterns and Best Practices is written with the intent to discuss issues that the author believes developers frequently run into when developing Backbone-based applications.
Conclusion and Recommendation

Backbone.js Patterns and Best Practices (Packt, 2014, 174 pages) is explicitly intended for intermediate to advanced level Backbone.js developers. For more introductory text to Backbone.js, other books that might be more appropriate (none of which I have read) include Beginning Backbone.js (Apress, 2013, 304 pages), Developing Backbone.js Applications: Building Better JavaScript Applications (O'Reilly, 2013, 374 pages, free open source reading), Backbone.js Cookbook (Packt, 2013, 282 pages), or Instant Backbone.js Application Development (Packt, 2013, 64 pages). There are also numerous online resources with introductory Backbone.js content. Because I have not reviewed these other Backbone.js books, I cannot definitively state that Backbone.js Patterns and Best Practices picks up where they leave off. However, my best guess is that there is some overlap between these seemingly more introductory Backbone.js books, but that Backbone.js Patterns and Best Practices likely covers some more advanced topics (patterns and best practices) not covered in those books.

No comments: