Using Backbone.js with CoffeeScript

If you want an example of just how elegant CoffeeScript can get, look no further then how it integrates with Backbone.js. In this tutorial we’re creating a model and a collection in Backbone.js in it’s simplest form, then expanding it to include API variable mapping - something you’re going to have to do when integrating with API’s in the future.


Create a model

Time to create a model. If you’re unfamiliar with what a model is in the context of a Model-View-Controller architectural pattern the simplest explanation is that a models only role is to store data. No data manipulation, no processing, no view manipulation and no user interaction processing.

OK that’s maybe a little too basic, let’s expand it with some default values.

Going through this line by line:


Creating a Backbone.js collection to add the models to

In Backbone.js collections in their simplest form are a way of storing multiple models of the same type.

Let’s break this down…


API integration

One of the best features of Backbone.js collections is that they support API integration out of the box with very minimal configuration. In this example I’m going to redeclare everything from this tutorial so you have a full view of everything required to create a Backbone.js model and collection, and integrate it with an API using CoffeeScript.

How simple is that…