backbone helpers to use cartodb SQL API
Latest commit a9fdbf3 Feb 9, 2012 @javi javi Update
Failed to load latest commit information.


This is a library that allows to access data from CartoDB using the SQL-API in the same way it works with REST. It is a read only library so you can not save data to CartoDB.


Add backbone.cartodb.js to your HTML. This library does not change Backbone.sync method so you could use it as usual if you have another models in your app from a REST API or localstorage.

First you have to create a CartoDB instance linked to your account:

    var CartoDB = Backbone.CartoDB({
        user: 'YOURUSER' // you should put your account name here

Then you have to define a collection pointing to your public table:

    var WifiPlaces= CartoDB.CartoDBCollection.extend({
        table: 'nyc_wifi', //public table
        columns: {
            'address': 'address',
            'type': 'type',
            'name': 'name',
            'location': 'the_geom'

columns dict tells which columns you want to fetch and the name are going to have in the model, so you can access it using that name:

var places = new WifiPlaces();
places.bind('reset', function() {
     places.each(function(p) {

Now you can do all you have been doing with Backbone models, that is, bind events, etc.

more features

PostGIS functions

if you map a column using a PostGIS function it will be mapped to na object:

columns: {
    'center': 'ST_Centroid(the_geom)',

then model.get('center') will return a point in GeoJSON format (as javascript object)


you could do:

places.where = "type = 'Free'"

so you will only get models with type = Free

raw sql

if you feel like real men, you can do this:

var MenList = CartoDB.CartoDBCollection.extend({
    sql: function() {
        return "select * from table";

all the columns will be mapped to model attributes. But be careful, geo types will not be converted.