Backbone and Cloudant

By Simon Metson

backbone.cloudant is a small library we've developed to make connecting to Cloudant from Backbone applications really easy. We hope it'll help others develop great web apps and would love to hear what people think of the first version, especially if they find bugs or have some feature requests.

Backbone

Backbone.js is a javascript framework that allows you to build well structured web applications. Data is held in models, organized in collections and rendered in views, in a manner that may seem familiar to people who know the Model-View-Controller pattern.

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.

We've been using Backbone in a number of projects over the past months (and are planning on using it a lot more in the future). It's a great fit for working with Cloudant straight from the browser.

Features

There are four obvious features for the backbone.cloudant library:

  • minimal dependencies: ideally require nothing more than Backbone itself
  • a base Model, configured to read/write directly to the database
  • simplify Collections relating to the Cloudant feature set: views, searches and _all_docs
  • a suitable change handler

I'm pleased to say that all of these features are in v0.0.1 of backbone.cloudant.

Because the Cloudant API is compatible with Apache CouchDB the code works with CouchDB, apart from where the feature set diverges. For example the SearchCollection is only supported against Cloudant because there's no native search feature in CouchDB. Everything else should work and have been tested against CouchDB.

Why not use an existing connector?

There are a lot of "connectors" available that hook Backbone up to a CouchDB backend, which of course work out of the box with Cloudant. These tend to override the Sync function, as suggested in the Backbone docs and allow you to talk to CouchDB or Cloudant from your Backbone app.

The Backbone docs describing the Sync function also say:

By default, it uses (jQuery/Zepto).ajax to make a RESTful JSON request and returns a jqXHR.

That means these connectors aren't really necessary. Backbone maps very nicely onto the REST API of CouchDB/Cloudant out of the box. In fact we've found these connectors tend to add complexity. We didn't want to make a connector like that, hence the barebones design of backbone.cloudant.

Grunt

You may notice that we're using grunt to build, document and deploy backbone.cloudant. Grunt is a great tool for managing javascript applications, and a great fit for what we wanted to do here. There'll be another blog post soon on using grunt in your own projects, but if you are into writing lots of javascript check it out!

Sign Up for Updates!

Recent Posts