Search code examples
javascriptbackbone.jscouchdb

Get Document By Id using CouchDB and Backbone


I am trying to follow a similar work flow as others using Backbone and CouchDB. If my connector looks like this:

Backbone.couch_connector.config.db_name = window.location.pathname.split("/")[1];
Backbone.couch_connector.config.ddoc_name = window.location.pathname.split("/")[3];
Backbone.couch_connector.config.global_changes = false;

And my URL has the document _id, whats the cleanest way to get a document?


Solution

    1. Define a route in your backbone router that passes the id to a function
    2. In the function instantiate the model, setting the passed in id
    3. Call fetch() on the model

      routes: {
          '/foo/:id' : 'doFoo'
      }
      
      doFoo = function(id) {
          doc = new Model({ '_id' : id }).fetch();            
      }
      

    http://jsfiddle.net/tuTAK/1/