Backbone + CoffeeScript
The introduction is oriented around two pressing questions: Why should you choose Backbone for your project? Why should you choose CoffeeScript? Despite our bias for this charming pair, we'll discuss the pros and cons of each, and mention some alternatives. Also, we'll explain a bit about the book: for whom it is intended, and how it should be read.
Backbone is just a small layer built on top of old friends like jQuery, which makes it easy to get started building rich client-side features of virtually any size. In this chapter we do just that, taking a whirlwind tour through Models, Views, Events, Collections, and Routers. When we're done, we'll have a working feature that we can run right in the chapter.
Setup and Structure
The goal of this chapter is to provide a guide to using Backbone in the real world. After covering the basics of getting started with both Backbone and CoffeeScript, we will then discuss the more advanced topics of how to organize your application and package it for delivery to the client.
Models and Domain Logic
Backbone Models are not difficult to understand, but by mixing in sync, validation, and events, they are powerful despite the apparent simplicity. Best of all, they give you a place to put your domain logic away from the DOM, where it can easily be understood, maintained, and tested.
Collections and Sync
Managing Models in a group is easy with Collections. The Collections api offers an extremely rich set of functions from Underscore, including map, pluck, reject, and shuffle. Also, just like Models, Collections provide a set of helpful Events that Views can observe. We will also take a look at backend integration.
Views and Events
Views bring your Backbone application to life. Building upon the Events infrastructure mentioned in the previous chapter, we can now 'wire up' our Views to respond to changes to the data in our Models. We will build an interactive feature that accepts user input, and demonstrates both nested and dispersed Views.
To remain manageable, templates must keep programming code to a minimum, and Eco kindly ushers in all of CoffeeScript's clean, readable power. In this chapter we'll get into more advanced templating topics with Eco, such as helper functions, localization, and nesting.
Routers and History
The Web is all about links, but until recently, most rich client-side Web applications lived at a single URL, making it impossible to share or bookmark links to important resources. Backbone's Router and History make it easy to develop a modern application that uses pushState (or hashtags) to keep the browser's address bar and history in the loop.
RESTful JSON APIs with Rails
Backbone can work with any Web server, but it was initially developed with Ruby on Rails, so Rails will be the subject of our in-depth look at integration. The approach is largely the same for any RESTful JSON API server, however, so no matter which platform you use, the material in this chapter will be relevant.