Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

backbone helpers to use cartodb SQL API

branch: master

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 example
Octocat-spinner-32 test
Octocat-spinner-32 README.md
Octocat-spinner-32 backbone.cartodb.js
README.md

Backbone.CartoDB

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.

usage

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
                       // YOURUSER.cartobd.com
    });

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.fetch();
places.bind('reset', function() {
     places.each(function(p) {
        console.log(p.get('address'));
        console.log(p.get('location'));
    });
});

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)

filter

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.

examples

Something went wrong with that request. Please try again.